GridBagLayout : Java Glossary


The most complicated layout manager, that gives you fine control over placement of subcomponents. You position them on a rough grid and the layout manager snuggles them up to each other. You can specify margins for each Component and which side/corner of its cell it should squeeze up to. The most puzzling parameter is weightx (weighty) which controls which cells grow when the entire layout acquires more real estate. 0 means no growth. The growth algorithm works like this. First it searches each column for the biggest desired growth. This becomes the desired growth for that column. Then the extra real estate apportioned to the columns relative to the growth weights. The growth weights need not be normalised percentages, but they could be. weighty works in a similar way.
You can have unused rows and columns, but not negative ones.

Typical Use

GridBagConstraints Fields

What are all those fields in the GridBagConstraints for? The best way to find out is just to experiment, possibly using GridBagger. Here are some rough hints.
Field Purpose
gridx column number. First column is column 0. It is ok to skip columns to leave room to add stuff later. They won’t take up space.
gridy row number. Top row is row 0. It is ok to skip rows.
gridwidth How many cells wide the Component is. May not be 0. This is not the number of pixels, but rather the number of cells. It can look as though nothing is happening if you increase the gridwidth for all the cells in a column since the extra column is zero-width. Rows must have different gridwidths in some cell for you to notice any difference between rows in cell size.
gridheight How many cells tall the Component is. May not be 0.
weightx What percentage of the extra width in the container should this Component take for its growth? Percentages don’t have to add up to 100. GridBagLayout normalises them for you, I have heard some talk what it does is actually more complicated. Note these are not the percentages for width, but the percentages for sharing the pie of any excess width left over after every column gets its minimum. Only cells using FILL will partake of the growth.
weightx What percentage of the extra height in the container should this Component take for its growth? Percentages don’t have to add up to 100.
anchor Where you want your Component to rest in its containing box. By default it is GridBagConstraints. CENTER. possibilities are: CENTER, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST and NORTHWEST.
fill GridBagConstraints.NONE = don’t grow the Component even if its allotted space is bigger than he Component. VERTICAL=grow the Component vertically to take up extra height. HORIZONTAL=grow the Component horizontally to take up extra width. BOTH=grow both vertically and horizontally.
ipadx How much wider you want your Component to be than it would be normally. Measured in pixels.
ipady How much taller you want your Component to be than it would be normally. Measured in pixels.
insets new Insets ( /* top */ 0 , /* left */ 0 , /* bottom */ 0 , /* right */ 0 ) The minimum amount of white space you demand around your Component. Measured in pixels.

If your GridBagLayout does not work, here are some things to check:

Here is my approach to writing GridBagLayouts:
  1. Draw a crude overall diagram, such as this one from my Quoter Amanuensis:
    // Applet Gridbag:
    //     0       1        2
    // --------title---  about  0
    // --------raw----------    1
    // -------cooked--------    2
    //  Charset  Clear  Convert 3
    //  target                  4
    //  trim                    5
    // -----instructions-----   6
  2. Label it with GridBagLayout coordinates.
  3. Get the gridx, gridy, gridheight and gridwidth parms right.
  4. Set your various alignment parameters, both within the Components and within the GridBagLayout’s anchor.
  5. If some Components are too small, stretch with fill.
  6. Put some ipadx and ipady around buttons to plumpen them up. This does not request more white space the way the Insets do, this leaves some room for the component itself to grow in. You would typically use it for a JLabel to account for the fact later setTexts might need a larger label. You want to leave room so the whole layout does not adjust every time you setText to a longer value. ipadx is the padding added to either end measured in pixels. It works by setting the adding ipadx * 2 to the mimimum width.
  7. Fine tune with insets to get the spacing right. Focus at first on getting the insets for the outer margins, then spacing between Components. In Swing, setting the outer margins is done much more easily using a border. It is very fiddly with insets. Also there’s an argument to use spacer components instead of insets. Insets get very fiddly, particularly when the Metal styleguide demands odd numbered spacings. Try it both ways and see which suits you better.
  8. Fiddle with weightx and weighty so that your layout still looks good when you grow or shrink it.
  9. Ask your artist lover to look at the design and tell you where it needs adjustment. Unfortunately mine left a decade ago, so my designs look quite wooden.


When using GridBagLayout in Swing there are a couple of tricks: You can’t do a setLayout on your JApplet directly. You must do it on the associated contentpane Container. Similarly you don’t add Components to the JApplet; you add them to the associated Container, e. g.
this.setLayout( gridBagLayout );
this.add( aButton, constraints );
Container contentPane = this.getContentPane();
contentPane.setLayout( gridBagLayout );
contentPane.add( aButton, constraints );
People complained so much about this clumsy syntax that in  Java version 1.5 or later you can now do your setLayout directly on the JApplet again if you choose.

Learning More

Oracle’s Javadoc on GridBagLayout class : available:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number