Java Reference
In-Depth Information
Table 17.2
The size, location and weight parameters for the six panels in the layout
of Figure 17.3
Panel
Width
Height
Left
Top
Weight
x
Weight
y
A
1
2
0
0
2.0
1.0
B
2
1
1
0
1.0
2.0
C
1
1
0
2
1.0
1.0
D
1
2
1
1
1.0
1.0
E
1
1
2
1
1.0
1.0
F
1
1
2
2
1.0
1.0
weight 1
0. We still have the problem that we cannot directly set the weights for
the rows or columns because rows and columns are internal concepts of the layout
manager. We can, however, assign two weights to the embedded components. The
horizontal weight (called weight
x
) affects the component's width and the vertical
one (called weight
y
) affects the component's height. The weight of a column is
then the maximum weight of a component in that column. Thus, by setting the
horizontal weight of panel
A
to 2
.
.
0 and that of panel
C
to 1
.
0, column 0 receives
weight 2
0.
The weight of the topmost row (row 0) is set by using a vertical weight of
.
2
0 for panel
B
. Using the vertical weight of panel
A
for this purpose would have
side-effects on row 1 because panel
A
spans rows 0 and 1. All other horizontal
and vertical weights are set to 1
.
0. Then row 1, row 2, column 1 and column 2 all
receive weight 1. The parameters are listed in Table 17.2. These settings will result
in the desired layout.
The column widths (or row heights) are then computed as follows. Let
w
0
,...,w
n
be the
n
.
=
w
0
+ ···+
w
n
be the sum of
the weights. Then the width of column
i
is a fraction of
+
1 column weights. Let
W
w
i
/
W
of the width of
the parent component. In our case
w
0
=
2
.
0,
w
1
=
1
.
0,
w
2
=
1
.
0 and
W
=
4
.
0.
Hence column 0 gets 2
.
0
/
4
.
0
=
1
/
2ofthe width of the parent component while
columns 1 and 2 each get 1
4. This shows that the absolute values of the
weights are irrelevant; only their ratio is important. We might have used the set
w
0
=
.
0
/
4
.
0
=
1
/
.
w
1
=
.
w
2
=
.
w
0
=
.
w
1
=
.
w
2
=
.
0
2,
0
1,
0
1or
6
0,
3
0,
3
0 without changing the
result.
17.1
■
The classes
GridBagLayout
and
GridBagConstraints
Two classes are needed to use a grid-bag layout, both of which are defined in the
library
java.awt
. Class
GridBagLayout
defines the actual layout manager. The
other class,
GridBagConstraints
,isahelper class to specify the size, dimension
and weight parameters for every component. Basically, an instance of
GridBag-
Constraints
represents one row of Table 17.2. Every component to be embedded
into a grid-bag layout has its own
GridBagConstraints
object.
Search WWH ::
Custom Search