Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any opinions on swing layouts?

Status
Not open for further replies.

riches85

Programmer
Nov 13, 2002
59
US
I have talked to several people lately about how hard it can be to layout components in swing so they are just the right size in the exact location you want them too. I was told by one person that GridBagLayout offers you the most power at the cost of complexity. A friend also said that he prefers to use embedded JPanels with simpler layout managers such as BorderLayout, GridLayout, and FlowLayout. The only problem I have found with either of these approaches is that they both generate bloated code. Like I said, I just want the components to be the right size. It seems that they all get greedy and want to take up all of the space alotted to them. I was kind of wondering if anybody out there with a great deal of experience with swing could offer me their opinions on the matter. What are the methods that are being employed in Industry? Is all of the bloated code necessary? Thank You all in advance

Kevin Zablonski
 
I personally use the simpler layouts with embedded panels within panels. This allows you to build your frame step by step. You end up with a lot of panels, it's true. But you can finesse some pretty nice GUIs with those few simple tools.

Remember that the layout managers buy you two things:

1) They automatically reposition things if the user changes the size of the frame
2) They give a better chance of your app working on multiple platforms.

If you have no intention of letting your users resize the frame, and you're only coding for one platform, then just use absolute positioning and be done with it. That's what I used to do and it worked for a long while.
 
I like to strengthen idarkes post.

If you internationalize your Strings, or like to change the names of labels, it's no problem, if you avoid absolute layout.

If you create your Panels in Methods, you may easily avoid bloated code.

Code:
JPanel makeFooPanel (String label, String text)
{
    JPanel jp = new JPanel ();
    jp.set... (...);
    // color, justification, ...
    return jp;
}

void foo ()
{
    JPanel jpAdress = new JPanel ();
    jpAdress.setLayout (new BoxLayout (BoxLayout.Y_AXIS));
    jpAdress.add (makeFooPanel ("Street", street);
    jpAdress.add (makeFooPanel ("Zip", zip);
    jpAdress.add (makeFooPanel ("City", city);
    // ...
}

This way, you get uniform Fields, and may change them at a single Point.

If there are Input-Fields of variable width, the user may get more room, by resizing the window.
I always get angry, when Programmer make (wrong) assumptions about my monitor-size, resolution, and force me to use a much too small window, which isn't resizeable, or only shows big grey borders, when I resize.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top