Java Reference
In-Depth Information
VARIABLE DESCRIPTION
area.
A value of GridBagConstraints.VERTICAL adjusts the height of the component to fill the display area.
A value of GridBagConstraints.BOTH adjusts the height and the width to completely fill the display
area.
This variable stores a reference to an object of type Insets . An Insets object defines the space allowed
between the edges of the components and boundaries of the display area it occupies. Four parameter val-
ues to the class constructor define the top, left-side, bottom, and right-side padding from the edges of the
component. The default value is Insets(0, 0, 0, 0) .
insets
If you don't intend to expand a component to fill its display area, you may still want to enlarge the com-
ponent from its minimum size. You can adjust the dimensions of the component by setting the following
GridBagConstraints instance variables:
TABLE 17-6: Variables for Enlarging the Component Size
VARIABLE DESCRIPTION
ipadx
An int value that defines the number of pixels by which the top and bottom edges of the component are
to be expanded. The default value is 0 .
An int value that defines the number of pixels by which the left and right edges of the component are to
be expanded. The default value is 0 .
ipady
If the component is still smaller than its display area in the container, you can specify where it should be
placed in relation to its display area by setting a value for the anchor instance variable of the GridBagCon-
straints object. Possible values are NORTH , NORTHEAST , EAST , SOUTHEAST , SOUTH , SOUTHWEST , WEST ,
NORTHWEST , and CENTER , all of which are defined in the GridBagConstraints class.
The last GridBagConstraints instance variables to consider are weightx and weighty , which are of
type double . These determine how space in the container is distributed between components in the hori-
zontal and vertical directions. You should always set a value for these; otherwise, the default of 0 causes
the components to be bunched together in the center of the container. The absolute values for weightx and
weighty are not important. It's the relative values that matter. If you set all the values the same (but not
zero), the space for each component is distributed uniformly. Space is distributed between components in
the proportions defined by the values of weightx and weighty .
For example, if three components in a row have weightx values of 1.0, 2.0, and 3.0, the first gets 1/6 of
the total in the x direction, the second gets 2/6, which is 1/3, and the third gets 3/6, which is half. The propor-
tion of the available space that a component gets in the x direction is the weightx value for the component
divided by the sum of the weightx values in the row. This also applies to the weighty values for allocating
space in the y direction.
I'm starting with a simple example of placing two buttons in a window and introducing another way of
obtaining a standard border for a component.
TRY IT OUT: Applying the GridBagConstraints Object
You can compile and execute the following program to try out the GridBagLayout manager:
Search WWH ::




Custom Search