Java Reference
In-Depth Information
2.
All components are added with the
add
method, as always, but they now specify a
GridBagConstraints
object as the second argument, which the layout manager will use to lay out
all the components. The
GridBagConstraints
object is a simple container for a number of fields
(
gridx
,
gridy
, and so on), and the same object can be reused to position all components.
3.
Try playing around with the constraints to guess what they do. Try changing some of them and observe
how they influence the look of the form. Try removing the
weightx
and
weighty
lines. What happens?
Now take a closer look at the fields in
GridBagConstraints
. You can set the following
GridBagConstraints
instance variables:
➤
gridx
and
gridy
: These integers specify the row and column of the cell where you want to
place the component. The upper leftmost cell has the position
gridx=0
and
gridy=0
. You
can also set this to
GridBagConstraints.RELATIVE
to specify that a component should fol-
low directly right of and/or under the component that was placed before.
➤
gridwidth
and
gridheight
: These integers specify the number of columns and rows the
component spans. The default value for these is 1 (the component takes up a single cell). You
can set this to
GridBagConstraints.REMAINDER
to specify that the component should take
up all the remaining space in the row and/or column.
➤
fill
: Used when the component's cell(s) span a larger area than the preferred size of the
component. When set to
GridBagConstraints.NONE
, the component's size will not be
stretched. When set to
GridBagConstraints.HORIZONTAL
,
VERTICAL
, or
BOTH
, the compo-
nent's size will be stretched accordingly (try playing with these in the previous Try It Out).
➤
ipadx
and
ipady
: These integers specify the internal padding of the component, with a default
value of 0. This determines how much space will be added to the size of the component.
➤
insets
: Add a
java.awt.Insets
object here to specify the external padding of the compo-
nent, i.e., the minimum amount of space between the component and the edges of the cell(s)
it resides in. By default, no external padding is used.
➤
anchor
: This field determines where to place the component when the size of the component
is smaller than its cell(s). Valid values are
GridBagConstraints.CENTER
(the default),
PAGE_
START
,
PAGE_END
,
LINE_START
,
LINE_END
,
FIRST_LINE_START
(top‐left corner),
FIRST_
LINE_END
(top‐right corner),
LAST_LINE_START
(bottom‐left corner), and
LAST_LINE_END
(bottom‐right corner). You can also use compass names (
NORTH
,
NORTHEAST
, and so on) if
you prefer, although those are considered to be deprecated.
➤
weightx
and
weighty
: These two values determine how the layout manager should distrib-
ute space among the columns and rows, which is important for resizing behavior. The official
Javadocs mention that specifying weights “is an art that can have a significant impact on the
appearance of the components.” In general, it is advisable to use either 0.0 or 1.0 as weights, and
use the numbers in between whenever necessary. Also important to know is that when you spec-
ify a weight of 0.0 (the default) for
all
components in the container, they will all clump together
in the center of the container. This is because the
GridBagLayout
will then put all extra space
between the edges of the container and the edges of the cell grid. Try removing the
weightx
and
weighty
lines from the previous Try It Out and check this out for yourself.
Search WWH ::
Custom Search