Java Reference
In-Depth Information
How It Works
Both vertical boxes are now contained in a
JPanel
container. Since
JPanel
objects are Swing
components, we can add a border, and this time we add a
TitledBorder
border that we create
directly using the constructor. A
TitledBorder
is a border specified by the first argument to the
constructor, plus a title that is a
String
specified by the second argument to the constructor. We use a
border of type
EtchedBorder
here, but you can use any type of border.
We introduce space between the two vertically aligned
Box
containers by adding a horizontal strut to
the
Box
container that contains them. If you wanted space at each side of the window, you could add
struts to the container before and after the components.
The last improvement is to the panel holding the buttons along the bottom of the window. We now
have a border that is composed of two types, 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 class,
createBlackLineBorder()
that creates a black line border that is one pixel wide, so
we could have used that here.
Using a GridBagLayout Manager
The
GridBagLayout
manager is much more flexible than the other layout managers we 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). A component can occupy more than one cell in
a row and/or column in the grid, but it 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 determined by its associated
GridBagConstraints
object.
A
GridBagConstraints
object has no less than eleven public instance variables that may be set to
define the constraints for a component. Since 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:
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.
Table continued on following page