GridBagLayout uses the 2-argument form of Container's add() method -
void add( Component c, Object constraints );The second parameter is an instance of class GridBagConstraints. [Note: BorderLayout also uses the 2-argument add() method - the second parameter is simply an instance of class String.]
All the members of class GridBagConstraints are public attributes, so they are assigned directly. You can routinely initialize the attributes on first use, and then reuse most of the settings for all subsequent invocations of add(). This means that only the constraints that must be changed are actually modified. [GridBagLayout makes its own copy of the constraints object passed to add(), so the programmer can reuse his/her copy.] GridBagConstraints' attributes are:
|gridx||the component's "x" cell location (0 is at the left)|
|gridy||the component's "y" cell location (0 is at the top)|
|gridwidth||the width of the component (in cells)|
|gridheight||the height of the component (in cells)|
|weightx||how the component's cells' widths expand to occupy any available space|
|weighty||how the component's cells' heights expand to occupy any available space|
|fill||how the component grows to fill its cell|
|anchor||how the component is placed in its cell if it doesn't fill all of the available space|
|insets||pixels of space to allocate above, left, below, and right of the component|
|ipadx||"padding" to be added to left and right of component when computing the minimum width for the cell|
|ipady||"padding" to be added to top and bottom of component when computing the minimum height for the cell|
gbc.gridx = GridBagConstraints.RELATIVE; p.add( new JButton("1"), gbc ); p.add( new JButton("2"), gbc ); p.add( new JButton("3"), gbc );gridwidth and gridheight. The constant GridBagConstraints.REMAINDER provides a similar shortcut for attributes gridwidth and gridheight. The tagged component will span the remainder of the row and/or column it has been assigned to. "This facility allows you to create components that span the right number of rows or columns even if you add extra components to the layout and change the number of components in the direction concerned." [Topley, p136]
weightx and weighty. These values specify how extra space in the container should be distributed among its components in the x and y dimensions. They do not have to add up to 1.0 in a given dimension. What is important is the ratio of a weightx value to the sum of all weightx values. If two or more components have the same weight value, they will "share equally in any extra pixels that come along". A weight of 0.0 means: the component should originally be laid-out right next to its neighbor, and, the component's cell should not be resized in that dimension when its container is resized.
"The weight feature is useful for the same reason that the border layout manager is useful: when the container resizes, some components should resize, some should resize in just one direction, and some should remain as they are." [Heller, p122]
fill. Specifies which dimensions of a component should grow when the space available for it is larger than its default size. The legal values are:
|GridBagConstraints.NONE||do not grow (the default)|
|GridBagConstraints.HORIZONTAL||grow horizontally only|
|GridBagConstraints.VERTICAL||grow vertically only|
|GridBagConstraints.BOTH||grow in both directions|
anchor. Defines how the component should be displayed within its grid cells when it is smaller than those cells. The legal values are:
insets. An object that encapsulates the margins to be used on all sides of the component.
ipadx and ipady. These fields specify internal padding to add on each side of the component in each dimension. They increase the size of the component beyond its default minimum size.
Apparently, GridBagLayout noticed that no buttons were added to row 3, so it did not allocate real estate for a row with no components (but it still created a "placeholder" internally [?] ), and, it did not give button 4 a height of two cells. Only when the window is enlarged vertically does button 4 start to exhibit a height that is greater than one.
Because of the weightx and weighty values for button 1, it should absorb all increases in window size. Notice that any component that shares the same row as the bottom of button 1, or the same column as the right side of button 1, grows in unison with button 1.
[Reference: Topley, pp129-150]