12.5. GridBagLayout

The GridBagLayout manager is about three times more flexible than any of the other standard layout managers. Unfortunately, GridBagLayout is also about nine times harder to use. The basic idea is that you chop up the window into grids, then you specify for each component which cell to start and end in. You can also specify how objects stretch when there is extra room and alignment within cells.

The basic steps to using GridBagLayout are:

Set the layout, saving a reference to it.

GridBagLayout layout = new GridBagLayout();

Allocate a GridBagConstraints object.

GridBagConstraints constraints =
  new GridBagConstraints();

Set up the GridBagConstraints for component 1.

 constraints.gridx = x1; constraints.gridy ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.