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 in panels - resize causes bunching up.. 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 in panels - resize causes bunching up.." Watch "GridBagLayout in panels - resize causes bunching up.." New topic

GridBagLayout in panels - resize causes bunching up..

Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
Hey guys,

Relatively new to Swing coding. I have an applicaiton with many panels, all using grid bags.. (I found it the easiest one to get things into the places i want them to be in). Everything looks nice when there is enough space, but when i crop the window to some very small size, instead of just not being able to see most of the panels, they seem to be attempting to dynamically resize to fit the window, and its all getting bunched up and overlapping jlabels etc, how do I stop this ?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46332
You aren't very specific about what your GUI looks like, but remember that GridBag is designed for resizing all the components of the display whenever you alter the size of the display.

Some people use a home-made GridBagConstraints class, which makes it much quicker to set up weights, etc. There is one on Cai Horstmann's website, where you add a component like this:

Now, you have to set the constraints properly; that is the hard part of using GridBad.
Remember that the gridX means the column number, and gridY means row number; I got those two the wrong way round at first and got no end of confusion.

The weight means how much you allow the size of each component to change as you alter the size of the whole display; the greater the weight, the more it changes. Since weight is demoninated in doubles rather than int, you can have a lot of variation in change.

The fill determines whether or not each component fills up its allotted space; you can get components far to small because you haven't given them a fill value.

The anchor determines which side or corner of the allotted space your component hangs onto; in my example it says NORTH, so the button will hang onto the top of its space.

The Insets determines how much empty space there is; in this case 5 px at left and right, and 10 at top and bottom, between the component and its allocation.

There is also internal padding, which I have never actually used myself, and gridwidth and gridheight which mean how many rows or columns you use for a particular component.

You will probably have to use fill and weight to get your display to behave itself.

Any use to you???
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46332
Did you get it to work?

Looking back at my post yesterday, I see it is full of spelling mistakes, and I have typed , instead of . Sorry.

I agree. Here's the link:
subject: GridBagLayout in panels - resize causes bunching up..
It's not a secret anymore!