0

i'm working on GUI in java and got stuck with move the object.

Please visit this youtube video i made a short demo for you guys to see what i was trying to do. I'm so new to the GUI thing as i've never been taught of doing GUI.

Here is the link: http://www.youtube.com/watch?v=up1LV5r-NSg

James P.
  • 19,313
  • 27
  • 97
  • 155
James1
  • 273
  • 1
  • 4
  • 10
  • 3
    +1 for the video & description. great idea! wish more people would do something like that. – Jan K. May 27 '10 at 05:59
  • One of the last sentences: *"I just need you now"* made me chuckle. :) – Bart Kiers May 27 '10 at 08:12
  • @Bart K: lolx didn't realized that, just thought it was a good song though! Although it was fitted with video, wasn't it? I do need you now to help! lolz – James1 May 27 '10 at 11:10
  • @james1, you're right, the song fitted perfectly with your "video-question"! I liked it. :) – Bart Kiers May 27 '10 at 11:36

3 Answers3

5

I see you're using a GUI designer. I highly recommend building your GUI "by hand" instead in which case your code is IMO much clearer (I'm not saying all GUI designers produce bad code, but it is almost always harder to read, and editing it will be hard without using the exact same GUI designer). Once you're comfortable with GUI designing by hand, then try a GUI designer and see what makes you more comfortable.

See: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

In your case, you might create a BorderLayout, and in the "south" of your panel/frame you can place a panel with a FlowLayout aligning it's components to the left. Then add your button to the panel with the FlowLayout.

A little demo:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class LayoutDemo extends JFrame {

    LayoutDemo() {
        super("LayoutDemo");
        super.setSize(400, 200);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createGUI();
        super.setVisible(true);
    }

    private void createGUI() {
        // set the layout of this frame
        super.setLayout(new BorderLayout());

        // create a panel to put the button on
        final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        // create a text area to put in the center
        final JTextArea textArea = new JTextArea();

        // create the search button
        final JButton searchButton = new JButton("search");

        // add a listener to the button that add some text to the text area
        searchButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText(textArea.getText() + "pressed search on " + (new Date()) + "\n");
            }
        });

        // add the button to the bottom panel
        bottomPanel.add(searchButton);

        // wrap a scroll-pane around the text area and place it on the center of this frame
        super.add(new JScrollPane(textArea), BorderLayout.CENTER);

        // put the bottom panel (containing the button) on the 'south' of this frame
        super.add(bottomPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LayoutDemo();
            }
        });
    }
}

produces:

alt text http://img689.imageshack.us/img689/5874/guiq.png


EDIT

And to move the button a bit more up, use the constructor new FlowLayout(FlowLayout.LEFT, int hgap, int vgap)

where hgap is the gap (in pixels) between the left and right components and vgap is the gap (in pixels) between the upper and lower components.

Try:

final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 10));

Note that the space between the button and text area also increases slightly!

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Upvoted for the BorderLayout suggestion. This is the solution I found by trial and error. http://stackoverflow.com/questions/2889300/swing-layout-using-a-grid-while-keeping-component-dimensions – James P. May 27 '10 at 10:48
  • So what would you change if you wanted to move the button up alittle bit more (just alittle bit more)? – James1 May 27 '10 at 11:14
  • From my experience, there are almost always bugs in handwritten code. Nothing can beat NetBeans Swing GUI builder (as for Java desktop) ... It takes little effort to understand how NetBeans 'thinks', once you achieve so you'll never get corrupted code ... – Xorty May 27 '10 at 11:54
  • 1
    @Xorty, I didn't say handwritten code is without bugs. But using a GUI designer without first knowing how to build a (simple) GUI "by hand" is IMO not the way to go. Also, a drawback of using a GUI designers is that others can't simply edit the GUI in an IDE/editor without that GUI designer. – Bart Kiers May 27 '10 at 12:09
  • @Bart K. - sounds better, I cancelled my downvote( edit : not really, stackoverflow wont let me atm:). As for learning - OK, as for something serious - no, never. Save your time (both for writting and debugging) and have team which knows, how to use IDE GUI designer ... – Xorty May 27 '10 at 12:11
  • @Xorty, never did a serious GUI, but you may have a point about "serious GUIs". FYI: you can only cancel a down-vote if the answer has been edited in the mean time (no problem, just leave the down vote where it is: I have enough rep-thingies. Always have, for that matter! :)). – Bart Kiers May 27 '10 at 12:26
  • @Bart K: Thanks alot for your help. I think i can make GUI and move some buttons around. Is this the way you can move buttons or textbox etc.? – James1 May 28 '10 at 02:29
  • @james1, you can place any GUI-component on a panel. I'm not quite sure what you mean by "moving around" though. But I highly recommend you look into the tutorial about layout managers first before continuing. Good luck. – Bart Kiers May 28 '10 at 06:29
3

learn fest swing test and miglayout! fest swing test enables you to run your gui screnario. and Miglayout,it is my opinion, also is easy to use layout lib.

Fest: http://fest.easytesting.org/swing/wiki/pmwiki.php
MigLayout: http://www.miglayout.com/
ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80
0

If you are not trying to learn the ins & outs of Java Swing and aren't trying to create some fancy GUI then a GUI designer like the one you are using should be fine.

What you are not able to do seems to be a limitation of your particular IDE though, and therefore it might be helpful to give Netbeans a try. You can always take the generated GUI code (ugly as it may be) and then plug it back into your project in your other IDE.

Hermann Hans
  • 1,798
  • 1
  • 13
  • 24
  • Yes, i've known that netbeans does that but i want to learn it in java and also understand the code, that's why i used that Jframe plug in thing to see how it's actually written. – James1 May 27 '10 at 10:59