Java Reference
In-Depth Information
Table 10-3.
The gridx, gridy, gridwidth, and gridheight Values for Figure 10-5
Component
gridx
gridy
gridwidth
gridheight
One
0
0
1
1
Two
1
0
1
1
Three
2
0
1
1
Four
0
1
2
1
Five
2
1
0
2
Six
0
2
1
1
Seven
1
2
1
3
It isn't necessary to set
gridx
and
gridy
to a specific location. If you set these fields to
RELATIVE
(the default), the system calculates the location for you. According to the Javadoc
comments, if
gridx
is
RELATIVE
, the component appears to the right of the last component
added to the layout. If
gridy
is
RELATIVE
, the component appears below the last component
added to the layout. However, this is misleadingly simple.
RELATIVE
placement works best if
you are adding components along a row or column. In this case, there are four possibilities for
placement:
•W th
gridx
and
gridy RELATIVE
, components are placed in one row.
•W th
gridx RELATIVE
and
gridy
constant, components are placed in one row, each to the
right of the previous component.
•W th
gridx
constant and
gridy RELATIVE
, components are placed in one column, each
below the previous component.
Varying
gridx
or
gridy
while setting the other field
RELATIVE
appears to start a new row,
placing the component as the first element of the new row.
•
If
gridwidth
or
gridheight
is set to
REMAINDER
, the component will be the last element of
the row or column occupying any space that's remaining. For the components in the rightmost
column of Table 10-3, for example, the
gridwidth
values could have been
REMAINDER
. Similarly,
gridheight
could be set to
REMAINDER
for the components in the bottom row.
gridwidth
and
gridheight
may also have the value
RELATIVE
, which forces the component
to be the next-to-last component in the row or column. Looking back to Figure 10-5, if button
six had a
gridwidth
of
RELATIVE
, button seven wouldn't appear because button five is the last
item in the row, and six is already next to last. If button five had a
gridheight
of
RELATIVE
, the
layout manager would reserve space below it so the button could be the next-to-last item in
the column.
Padding
The
insets
value specifies the external padding in pixels around the component (the space
between the component and the edge of the cell or cells allotted to it). An
Insets
object can
specify different padding for the top, bottom, left, or right side of the component.