Taming the GridBagLayout

Of the standard layout managers of the Java SDK, the GridBagLayout is the most useful one, but its complexity has also been known to strike fear in the hearts of programmers. Part of the complexity lies in the tedium of setting up the grid bag constraints. 

Consider for example the program in http://java.sun.com/docs/books/tutorial/uiswing/events/example-swing/ContainerEventDemo.java. (To run the program, click here [with a Java 2 enabled browser, of course]).

Can you figure out from the init() method why the grid bag constraints result in the given layout?

There are two problems with this code.

Some programmers make a new GridBagConstraints object for each component and populate it with the non-default settings. That's easier to read, but the code is quite bloated. Here is a typical example.

What the world needs is a more convenient method of setting the grid bag constraints. I developed a convenience class GBC that solves this problem. Here is a usage example that produces the same layout as the one from the Java tutorial:
        contentPane.add(scrollPane, 
new GBC(0, 0)
.setSpan(2, 1)
.setFill(GBC.BOTH)
.setWeight(0, 1.0));

contentPane.add(clearButton,
new GBC(0, 1)
.setSpan(2, 1));

contentPane.add(addButton,
new GBC(0, 2)
.setWeight(1.0, 0));

contentPane.add(removeButton,
new GBC(1, 2)
.setWeight(1.0, 0));

contentPane.add(buttonPanel,
new GBC(0, 3)
.setFill(GBC.BOTH)
.setSpan(2, 1)
.setWeight(0, 1.0));
As you can see, the component positions and the various constraints are easy to spot. (Keep in mind that the grid bag coordinates are x/y values and not row/column values.)

The GBC helper class uses two tricks:
None of this is rocket science, but I think it is a clean and useful mechanism that should make the GridBagLayout less scary.



Download GBC.java.