Java Reference
In-Depth Information
left.add(rbutton);
left.add(Box.createVerticalStrut(30)); // Space between
radioGroup.add(rbutton = new JRadioButton("Blue"));
left.add(rbutton);
left.add(Box.createVerticalStrut(30)); // Space between
radioGroup.add(rbutton = new JRadioButton("Yellow"));
left.add(rbutton);
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. We 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"));
If you run the example with these changes the window
will look like this:
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 that is available. The distribution of
surplus space vertically is different in the two columns because the number of components is different.
We 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.
While the name gives the impression that it binds components together, it, in fact, provides an elastic
connector between two components that can expand or contract as necessary, so it acts more like a
spring. Glue components can be placed between the actual 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 added. If you wanted all the surplus space to be at the beginning of a Box
container, for instance, you should first add a single glue component in the container.
Search WWH ::




Custom Search