aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Placing components with Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Placing components with Swing" Watch "Placing components with Swing" New topic
Author

Placing components with Swing

Rasta Kalo
Greenhorn

Joined: May 20, 2007
Posts: 5
Hello,

A few months ago I started on Java, and now as a test I'm trying to create interfaces with Swing, the problem is that I can't seem to succeed in placing the widgets on the appropriate position.
I've spend hours to get it to work, but either the widgets are partially displayed on the screen or they've become huge.

Below I've placed a part of the code(only the most relevant stuff).

Mijn question is what am I doing wrong?

tia

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Rasta Kalo
Greenhorn

Joined: May 20, 2007
Posts: 5
Thanks for your reply Craig Wood
I used your adjustments, however I'm still getting huge textfields, see below:


[ May 21, 2007: Message edited by: Rasta Kalo ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
GridLayout does this. You can try different layout managers.
See Lesson: Laying Out Components Within a Container.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I believe that Layout Managers are more trouble than they're worth -- especially for those who need it most, Java newbies like me.

You can disable Layout managers by setting the layout to null. I did this twice (though I'm not sure it was necessary to do it twice, Swing is working for me now).



Then you have complete control over the size and location of your components using:

setSize();
and
setLocation:

or

setBounds();

It was hard for me to believe but I found that layout managers would override my calls to setBounds() !

If you want to use layout managers, I believe that -- for some types of Components -- you need to override getMinimumSize(), getPreferredSize(), and getMaximumSize().

Kaydell
[ May 21, 2007: Message edited by: Kaydell Leavitt ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I disagree with the poster above... using "null" layout is more problems than it's worth - you have to do *all* sizing by hand... and good luck getting your GUI running on any computer with different resolution/screen size/font size than the one you developed it on - or even being able to resize the window.

You can build a GUI using nested panels - the best layouts to use here are BorderLayout, FlowLayout, GridLayout, and BoxLayout. If you're having weird sizing problems the issue can usually be resolved by replacing your component with a new panel holding your component with a different layout.

GridBagLayout is more for "overall" layouts (as opposed to nested panels - you layout all components on the screen at once using one layout - though of course you can combine *any* layout manager in a nested manner, this is just usually the way I've seen them used.), and in my opinion, is way more trouble than it's worth.

SpringLayout is also more for "overall" layouts, and is more for use in GUI builders - it's difficult to figure out the "correct" spring settings without being able to see it change on the fly.

There's also JGoodies Forms also known as "FormLayout", which is a really good layout manager for stuff you used to have to use GridBagLayout for.

There are now also some good GUI builders (like the Netbeans GUI builder (aka Project Matisse) - but I would recommend learning the basics of how GUI development is done in Java before relying on a GUI builder - and layout is one of these basics.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I'll have to read-up on the other dozen or so layout managers besides the manual one (null).

It sounds like experienced Swing developers need to know how to use the layout managers besides null.

Kaydell
[ May 22, 2007: Message edited by: Kaydell Leavitt ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Placing components with Swing