Java Reference
In-Depth Information
TryBoxLayout2.java
The extra statements add a 30-pixel vertical strut at the start of the columns, and a further strut of the
same size between each radio button and the next. You can do the same for the checkboxes:
// Create right columns of checkboxes
Box right = Box.createVerticalBox();
right.add(Box.createVerticalStrut(30));
// Starting space
right.add(new JCheckBox("Dashed"));
right.add(Box.createVerticalStrut(30));
// Space between
right.add(new JCheckBox("Thick"));
right.add(Box.createVerticalStrut(30));
// Space between
right.add(new JCheckBox("Rounded"));
TryBoxLayout2.java
If you run the example with these changes, the window looks like the one shown in Figure 17-24 .
FIGURE 17-24
It's better, but far from perfect. The columns are now equally spaced in the window because the vertical
struts have assumed a width to take up the excess horizontal space. The distribution of surplus space vertic-
ally is different in the two columns because the number of components is different. You can control where
surplus space goes in a Box object with glue . Glue is an invisible component that has the sole function of
taking up surplus space in a Box container.
Although the name glue gives the impression that it binds components together, in fact glue provides an
elastic connector between two components that can expand or contract as necessary, so it acts more like a
spring. You can place glue components between the components in the Box and at either or both ends. Any
surplus space that arises after the actual components have been accommodated is distributed between the
glue components. If you want all the surplus space to be at the beginning of a Box container, for example,
you should first add a single glue component in the container.
You create a component that represents glue by calling the createGlue() method for a Box object. You
then add the glue component to the Box container in the same way as any other component wherever you
 
 
Search WWH ::




Custom Search