Java Reference
In-Depth Information
To improve the layout of the application window, we 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 horizontally. 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 container, you can force a minimum width on the container. You can use a
vertical strut in a horizontal box to force a minimum height.
Vertical Box Container
This vertical strut forces
a minimum distance
between the top of the
vertical and the first
component
Red
Box
Vertical struts can be
used in a vertical to
force a specific distance
between one component in
the container and the next
Box
Green
Blue
You can use a horizontal
strut in a vertical to
force the to have a
minimum width
Box
Yellow
Box
Note that although vertical struts have zero width, they have no maximum width so they can expand
horizontally to have a width that takes up any excess space. Similarly, the height of a horizontal strut
will expand when there is excess vertical space available.
A vertical strut is returned as an object of type Component by the static createVerticalStrut()
method in the Box class . The argument specifies the height of the strut in pixels. To create a horizontal
strut, you use the createHorizontalStrut() method.
We can space out our radio buttons by inserting struts between them:
// Create left column of radio buttons
Box left = Box.createVerticalBox();
left.add(Box.createVerticalStrut(30)); // Starting space
ButtonGroup radioGroup = new ButtonGroup(); // Create button group
JRadioButton rbutton; // Stores a button
radioGroup.add(rbutton = new JRadioButton("Red")); // Add to group
left.add(rbutton); // Add to Box
left.add(Box.createVerticalStrut(30)); // Space between
radioGroup.add(rbutton = new JRadioButton("Green"));
Search WWH ::




Custom Search