CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Layout

Controls and layout managers
Layout managers
Constraints for a GridBagLayout

Each GridBagLayout uses a dynamic rectangular grid of cells, with each component occupying one or more cells called its display area. Each component managed by a GridBagLayout is associated with a GridBagConstraints instance that specifies how the component is laid out within its display area. How a GridBagLayout places a set of components depends on the GridBagConstraints and minimum size of each component, as well as the preferred size of the component's container.

To use GridBagLayout effectively, you must customize the GridBagConstraints of one or more of its components. You customize a GridBagConstraints object by setting one or more of its public instance variables. These variables specify the component location, size, growth factor, anchor, inset, filling, and padding.

Fields of the GridBagConstraints class

Field Description
gridx An int value that specifies the leftmost cell that the component occupies. gridx specifies the column in which the component will be placed.
gridy An int value that specifies the topmost cell that the component occupies. gridy specifies the row in which it will be placed.
gridheight An int value that specifies the number of vertical cells that a component occupies.
gridwidth An int value that specifies the number of horizontal cells that a component occupies.
ipadx An int value that specifies the amount of internal horizontal padding to be added to each control.
ipady An int value that specifies the amount of internal vertical padding to be added to each control.
insets An Insets object that specifies the amount of empty space to leave on each side of the cell.
weightx A double value that specifies how extra horizontal space is distributed if the resulting layout is horizontally smaller than the area allotted.
weighty A double value that specifies how extra vertical space is distributed if the resulting layout is vertically smaller that the area allotted.
anchor An int value that specifies the alignment of a component within a cell.
fill An int value that specifies what to do with extra space in a cell.

Fields of the GridBagConstraints class that set the anchor field

CENTER NORTHEAST WEST SOUTHEAST NORTH EAST SOUTH

Fields of the GridBagConstraints class that set the fill field

NONE HORIZONTAL VERTICAL BOTH

Other fields of the GridBagConstraints class

Field Description
RELATIVE For the gridx and gridy fields, this field specifies that the component will be placed next to the last added component. For the gridwidth and gridheight fields, this field specifies that the component will be the next-to-last component in a row or column.
REMAINDER For the gridwidth and gridheight fields, this field specifies that a component is the last component in a row or column.

A constructor for the Insets class

Constructor Description
Insets(intTop, intBottom, intLeft, intRight) Creates an Insets object with the specified spacing for the top, bottom, left, and right of each cell.

Note

  • In most cases, you should set the insets field to create some visual space between cells. Otherwise, the components in the layout will appear jammed together.

Previous | Summary | GridBagLayout manager | Constraints for a GridBagLayout | Application