JFrame f = new JFrame("Vertical BoxLayout-managed container");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = new BoxPanel();
f.setContentPane(pane);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
JButton button = new JButton("X Alignment = " + align);
button.setAlignmentX(align);
pane.add(button);
pane.add(Box.createVerticalGlue());
}
f.setSize(400, 300);
f.setVisible(true);
}
}
Executing this code produces results like those shown below, where the extra vertical space is distributed
evenly to each of the glue components:
As mentioned earlier, extra vertical space is distributed by a vertical BoxLayout based on the difference
between a component's maximum vertical size and its preferred vertical size. As you might guess, glue
components are simply "dummy" components with a large maximum size and a minimum size of 0, so in
many cases, all extra space will be assigned to them. However, as we saw earlier with JTextField
instances, it's possible for other components with large maximum sizes to accidentally be made
inappropriately large by a BoxLayout, and this can occur even when glue components are used.
Struts
One definition of the word "strut" in Webster's New World dictionary is, "a brace fitted into a framework to
resist pressure in the direction of its length", and unlike glue components, struts are appropriately named.
Struts are similar to rigid areas, but with an important difference: instead of specifying both the width and
height of the component, you only specify a strut's size in one dimension.
Specifically, you specify the width when you call createHorizontalStrut() and the height when
calling createVerticalStrut(). The strut uses the value you specify for its minimum, preferred, and
maximum size in that dimension, and uses 0 for the other dimension when setting its minimum and preferred
size. However, when setting the maximum size, Box uses a very large value for the remaining dimension
(width for a vertical box, height for a horizontal box), and this can cause undesirable results. Specifically, the
presence of a very large strut component in the BoxLayout can result in its container being assigned a size
that's larger than what was intended.
Since rigid areas can provide the same functionality and because there is a potential problem associated with
the use of struts, you should avoid struts and use rigid areas instead.
Stop by in one week for the next installment!
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.