Java Reference
In-Depth Information
The last improvement is to the panel holding the buttons along the bottom of the window. You now have
a border that is composed of two borders, one inside the other: a LineBorder and a BevelBorder . A
CompoundBorder object defines a border that is a composite of two border objects, the first argument
to the constructor being the outer border and the second being the inner border. The LineBorder class
defines a border consisting of a single line of the color specified by its first constructor argument and a
thickness in pixels specified by the second. There is a static method defined for the LineBorder class,
createBlackLineBorder() , that creates a black line border that is one pixel wide, so you could have
used that here.
Using a GridBagLayout Manager
The java.awt.GridBagLayout manager is much more flexible than the other layout managers you have
seen and, consequently, rather more complicated to use. The basic mechanism arranges components in an
arbitrary rectangular grid, but the rows and columns of the grid are not necessarily the same height or width.
A component is placed at a given cell position in the grid specified by the coordinates of the cell, where the
cell at the top-left corner is at position (0,0). You can spread a component over several cells in a row and/or
column in the grid, but a component always occupies a rectangular group of cells.
Each component in a GridBagLayout has its own set of constraints. These are defined by an object of
type GridBagConstraints that you associate with each component before adding the component to the
container. The location of each component, its relative size, and the area it occupies in the grid are all de-
termined by its associated GridBagConstraints object.
A GridBagConstraints object has no less than 11 public instance variables that may be set to define the
constraints for a component. Because they also interact with each other, there's more entertainment here than
with a Rubik's cube. Let's first get a rough idea of what these instance variables in a GridBagConstraints
object do (as listed in Table 17-4 ) :
TABLE 17-4 : GridBagConstraints Object Instances
INSTANCE
VARIABLE
DESCRIPTION
gridx and gridy Determines the position of the component in the container as coordinate positions of cells in the
grid, where (0,0) is the top-left position in the grid.
gridwidth and
gridheight
Determines the size of the area occupied by the component in the container.
weightx and
weighty
Determines how free space is distributed between components in the container.
anchor Determines where a component is positioned within the area allocated to it in the container.
ipadx and ipady Determines by how much the component size is to be increased above its minimum size.
fill
Determines how the component is to be enlarged to fill the space allocated to it.
Specifies the free space that is to be provided around the component within the space allocated to it
in the container.
insets
All that should seem straightforward enough. You can now explore the possible values you can set for
these and then try them out.
GridBagConstraints Instance Variables
 
 
Search WWH ::




Custom Search