Skip to main content
gamedev.net
🔒 Locked

[java] flowlayout question

Started by marcmassacre Dec 28, 2006 at 6:31 PM 5 replies 900+ views
Original Post
marcmassacre
marcmassacre
How to I specify the exact location I want a button or any type of component?
TheUltimatePirate
TheUltimatePirate
I know the topic says "flowlayout question", but if you want to specify an exact location for a component just use AbsoluteLayout. This is usually used for dialogs that have resizing disabled.

If you want components that can resize based on the size of the frame, you'll have to use one of the more difficult layout managers, such as GridBagLayout. I suggest using NetBeans, which has a sweet GUI builder called Matisse. It uses GroupLayout by default, which just became a standard layout in Java6. So if you're developing a Java app using 1.4 or 1.5 you'll need to bundle an additional JAR file (NetBeans takes care of this for you). There's also an eclipse port of Matisse.

If you prefer coding the GUI by hand, check out MIGLayout.
marcmassacre
marcmassacre
Thanks for your help...

But how could I specify exactly where I want a button to be on the screen? For instance...

add(new Button("cancel", x, y));


is there anything like that or any way I can do that?
Angex
Angex
You can use the setBounds() method (Should work with Swing and AWT) , but you first need to make sure the parent container doesn't use any layout manager (It picks one by default).

This Trail has some really good information on Layout Managers.

For Example:

final JPanel parent = new JPanel();parent.setLayout(null);final JButton child = new JButton("Button");// setBounds(x, y, width, height)child.setBounds(0, 0, 100, 50);parent.add(child)
CaptainJester
CaptainJester
This is not recommended.

Why do you want to do this?
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
TheUltimatePirate
TheUltimatePirate
Quote:
Original post by marcmassacre
Thanks for your help...

But how could I specify exactly where I want a button to be on the screen? For instance...

add(new Button("cancel", x, y));


is there anything like that or any way I can do that?


I'm sorry, I thought AbsoluteLayout was a standard Java layout but it's actually part of NetBeans (I've never used it myself). If you use it, you will have to bundle an additional JAR. But it does exactly what you asked.

This example places a new JButton at x=20, y=10.
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());getContentPane().add(new JButton("OK"), new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));
TheUltimatePirate
TheUltimatePirate
Not to sound like a netbeans fanboy, but just to show you how powerful NetBeans gui creator is, I created this IE 7 style dialog in just a few minutes since my last post:



While it's not perfect since I threw it together quickly, it can easily be resized unlike a dialog created using AbsoluteLayout.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.