Java Reference
In-Depth Information
Table 7.1
GridBagConstraints
Parameters.
Parameter
Description
Values
Cell column, row number
Values start at 0,0
int gridx, gridy
Number of rows and columns
taken up by the component
≥1
int gridheight, gridwidth
Allotment weight for setting
width & height of a cell
Arbitrary scale. Settings go
according to relative values.
See text for more details
int weightx, weighty
Determines whether
component fills cell in x, y,
both, or none
HORIZONTAL, VERTICAL,
BOTH, NONE
int fill
Position of component within
its cell when fill is not set to
BOTH
CENTER, EAST, SOUTHEAST,
SOUTH, SOUTHWEST,
WEST, NORTHWEST,
NORTH, NORTHEAST
int anchor
Padding added between a
component and cell borders
≥
0
int ipadx, ipady
Defines padding between
components
≥0
Insets insets
you set the
gridx
and
gridy
integer variables in the
GridBagConstraints
object for the cell position of the component. The layout examines these values
for all the components to find the number of cells needed in the horizontal and
vertical dimensions.
Table 7.1 lists the parameters in
GridBagConstraints
along with descrip-
tions and allowed values. The purpose of the parameters is straightforward except
for
weightx
and
weighty
. These two parameters determine the allocation of
the available space for a column's width and a row's height when the container
is resized. If the weights are all the same then the column and row widths will
be the same (except for the variations due to the different minimum widths and
heights of components in the cells).
Yo u can vary the weight values to force the columns and rows to span differ-
ent widths and heights. For example, the column with the cell with the largest
weightx
gets the biggest allocation of horizontal space. The column with the
next biggest
weightx
gets the next biggest amount of horizontal space. And so
on. The
weighty
values work in a similar manner for vertical space allocations
to the rows.
The following code leads to the layout shown in Figure 7.13. The program
demonstrates the use of several of the
GridBagConstraints
parameters. Note
that the same
GridBagConstraints
object is used throughout. Only the cell
coordinates and one or more of its other values are changed when it is used with
the addition of the next component.
Figure 7.13
Arranging
components with the
GridBagLayout
.
Search WWH ::
Custom Search