Java Reference
In-Depth Information
A component occupies at least one grid position, or cell , in a container that uses a GridBagLayout object,
but it can occupy any rectangular array of cells. The total number of rows and columns, and thus the cell
size, in the grid for a container is variable and determined by the constraints for all of the components in the
container. Each component has a specified position in the grid plus the area it is allocated within the grid
defined by a number of horizontal and vertical grid positions. This is illustrated in Figure 17-27 .
FIGURE 17-27
The top-left cell in a layout is at position (0,0). You specify the position of a component by defining
where the top-left cell that it occupies is, either relative to the grid origin or relative to the last component
that was added to the container. You specify the position of the top-left cell that a component occupies in
the grid by setting the gridx and gridy members of the GridBagConstraints object to appropriate values
of type int . The default value for gridx is GridBagConstraints.RELATIVE — a constant that places the
top-left grid position for the component in the column immediately to the right of the previous component.
The same value is the default for gridy , which places the next component immediately below the previous
one.
You specify the number of cells to be occupied by a component horizontally and vertically by setting
values for the gridwidth and gridheight instance variables for the GridBagConstraints object. The de-
fault value for both of these is 1 . You can use two constants as values for these variables. The GridBagCon-
straints.REMAINDER value makes the component the last one in the row or column and occupies the re-
maining cells. If you specify the value as GridBagConstraints.RELATIVE , the component is the penultim-
ate one in the row or column.
If the preferred size of the component is less than the display area, you can control how the size of
the component is adjusted to fit the display area by setting the fill and insets instance variables for the
GridBagConstraints object, as shown in Table 17-5 :
TABLE 17-5 : Variables for Adjusting Component to Display Area
VARIABLE DESCRIPTION
fill
The value for this variable is of type int , and it determines how the size of the component is adjusted in
relation to the array of cells it occupies. The default value of GridBagConstraints.NONE means that the
component is not resized.
A value of GridBagConstraints.HORIZONTAL adjusts the width of the component to fill the display
 
 
 
 
Search WWH ::




Custom Search