File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes GridBagLayout Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GridBagLayout Problem" Watch "GridBagLayout Problem" New topic

GridBagLayout Problem

Axel Leucht

Joined: Jan 19, 2009
Posts: 21
I do have a problem with the GUI-Layout.

Problem is: Initially everything looks ok. But whenever I resize the panel to a very small size, the JTextfields "jump" to their minimum size. which looks very bad.

Here is some code to demonstrate what I mean and I would appreciate any help on this issue.


pete stein

Joined: Feb 23, 2007
Posts: 1561
Perhaps setting weights of your components will help. Myself, I would avoid GridBagLayout unless forced. Instead I nest simpler layouts. For instance:
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Try adding your panel to JScrollPane, that way components wont slide to left when resizing and you may want to set preferred size for labels.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this seems to work OK, but GridBagLayout has a habit of 'fixing one thing, breaks another 2'
so just needs a lot of testing to see how it stands up.

1) get rid of the column setting for each textfield

2) include an ipadx of 100 (2nd last argument), but only for the 4 textFields
//new Insets(5, 5, 0, 5), 0, 0));
new Insets(5, 5, 0, 5), 100, 0));

this should get you close to the size you want for the textfields, if not, modify the 100
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10824

Check out MigLayout if you are interested.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: GridBagLayout Problem
It's not a secret anymore!