views:

129

answers:

4

Hi guys, 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

A: 

I am not sure which IDE you are using.. You can use Netbeans or Eclipse to do these kind of stuff very easily! Why are you not using Netbeans of Eclipse?

Abdel Olakara
To me this should be a comment rather then an answer
Andreas_D
Also, it's quite obviously Eclipse.
BoltClock
Yes, I've known about the netbeans but i'm trying to do this in Eclipse.
James1
He is using Eclipse
Xorty
+5  A: 

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


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
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.
So what would you change if you wanted to move the button up alittle bit more (just alittle bit more)?
James1
@james1, see my EDIT.
Bart Kiers
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
@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
@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
@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
@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
@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
A: 

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.

Herminator
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
+1  A: 

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
It looks great, will try to learn this. Thanks alot
James1
i used them my graduation project. The major problem of GUI development is Layout problem. But mig enables you to make good design without any visual editor.
ibrahimyilmaz