Java Reference
In-Depth Information
arrangement. The
Box
object for the right-hand group of
JCheckBox
objects works in the same way as
that for the radio buttons.
Both the
Box
objects holding the columns are added to another
Box
object that implements a horizontal
arrangement to position them side-by-side. Note how the vertical
Box
objects adjust their widths to match
that of the largest component in the column. That's why the two columns are bunched toward the left
side. You see how to improve on this in a moment.
You use a
JPanel
object to hold the buttons. This has a flow layout manager by default. Calling the
setPreferredSize()
method for each button sets the preferred width and height to that specified by the
Dimension
object
size
. This ensures that, space permitting, each button is 80 pixels wide and 20 pixels
high.
I have introduced another way of obtaining a border for a component here. The
javax.swing.BorderFactory
class contains static methods that return standard borders. The
cre-
ateBevelBorder()
method returns a reference to a
BevelBorder
object as type
Border
—
Border
be-
ing an interface that all border objects implement. You use this border for each of the buttons. You try
more methods in the
BorderFactory
class later.
To improve the layout of the application window, you can make use of some additional facilities provided
by a
Box
container.
Struts and Glue
The
Box
class contains static methods to create an invisible component called a
strut
. A vertical strut has a
given height in pixels and zero width. A horizontal strut has a given width in pixels and zero height. The
purpose of these struts is to enable you to insert space between your components, either vertically or ho-
rizontally. By placing a horizontal strut between two components in a horizontally arranged
Box
container,
you fix the distance between the components. By adding a horizontal strut to a vertically arranged
Box
con-
tainer, you can force a minimum width on the container. You can use a vertical strut in a horizontal box to
force a minimum height. The way in which you might use struts to fix the vertical spacing between radio
buttons is illustrated in
Figure 17-23
.