GridBagConstraints

GridBagConstraints are the meat behind the GridBagLayout; they specify how to display components. Unlike other layout managers, which have a built-in idea about what to do with their display, the GridBagLayout is a blank slate. The constraints attached to each component tell the layout manager how to build its display.

Every Component added to a GridBagLayout has a GridBagConstraints object associated with it. When an object is first added to the layout, it is given a default set of constraints (described later in this section). Calling setConstraints() (or add(Component, GridBagConstraints)) applies a new set of constraints to the object. Most people create a helper method to make the setConstraints() calls, passing constraint information as parameters. The helper method used in Example 7.2 follows:

public static void addComponent (Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int fill, int anchor) throws AWTException {
 LayoutManager lm = container.getLayout(); if (!(lm instanceof GridBagLayout)) {
 throw new AWTException ("Invalid layout" + lm);
}
else {
 GridBagConstraints gbc = new GridBagConstraints (); gbc.gridx = gridx; gbc.gridy = gridy; gbc.gridwidth = gridwidth; gbc.gridheight = gridheight; gbc.fill = fill; gbc.anchor = anchor; ((GridBagLayout)lm).setConstraints(component, gbc); container.add (component);
}
} 

In Java 1.1, you can make this method slightly cleaner by adding the component and applying the constraints in the same call to add(). To do so, replace the lines calling setConstraints() and add() with this line:

 // Java 1.1 only container.add(component, gbc); 

GridBagConstraints Methods

Constants and variables

Figure 7.11: GridBagLayout with fill values of VERTICAL, HORIZONTAL, and NONE

[Graphic: Figure 7-11]

Demonstrating gridx/gridy/gridwidth/gridheight
Component gridx gridy gridwidth gridheight
One
Two
Three
Four
Five
Six
Seven
Constructors
GridBagConstraints Defaults.
Variable Value Description
anchor CENTER If the component is smaller than the space available, it will be centered within its region.
fill NONE The component should not resize itself if extra space is available within its region.
gridx RELATIVE The component associated with this constraint will be positioned relative to the last item added. If all components have gridx and gridy RELATIVE, they will be placed in a single row.
gridy RELATIVE The component associated with this constraint will be positioned relative to the last item added.
gridwidth 1 The component will occupy a single cell within the layout.
gridheight 1 The component will occupy a single cell within the layout.
insets 0x0x0x0 No extra space is added around the edges of the component.
ipadx 0 There is no internal padding for the component.
ipady 0 There is no internal padding for the component.
weightx 0 The component will not get any extra space, if it is available.
weighty 0 The component will not get any extra space, if it is available.
Miscellaneous methods