Java Reference
In-Depth Information
Using the no-argument constructor for
GridBagConstraints
starts with all the defaults in
Table 10-2. You can leave the individual settings alone and just set the individual fields. All are
public, without getter methods. While you can just blindly pass in all the constraints to the
GridBagConstraints
constructor, it is better to describe the different fields separately.
Table 10-2.
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 avail-
able 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 wide within the layout.
gridheight
1
The component will occupy a single cell high within the layout.
insets
0x0x0x0
No extra space is added around the edges of the component.
ipadx
0
There is no internal horizontal padding for the component.
ipady
0
There is no internal vertical padding for the component.
weightx
0
The component will not get any extra horizontal space,
if available.
weighty
0
The component will not get any extra vertical space, if
available.
Component Anchoring
The
anchor
variable specifies the direction in which the component will drift in the event it is
smaller than the space available for it.
CENTER
is the default. The absolute values are
NORTH
,
SOUTH
,
EAST
,
WEST
,
NORTHEAST
,
NORTHWEST
,
SOUTHEAST
, and
SOUTHWEST
. The relative values are
PAGE_START
,
PAGE_END
,
LINE_START
,
LINE_END
,
FIRST_LINE_START
,
FIRST_LINE_END
,
LAST_LINE_START
, and
LAST_LINE_END
.
Component Resizing
The value of
fill
controls the component's resize policy. If
fill
is
NONE
(the default), the layout
manager tries to give the component its preferred size. If
fill
is
VERTICAL
, it resizes in height if
additional space is available. If
fill
is
HORIZONTAL
, it resizes in width. If
fill
is
BOTH
, the layout