Note that ascent and descent values can be calculated from a component's preferred, minimum, or maximum
sizes, and as we'll see, each one plays a role in BoxLayout's behavior. In addition, the "ascent" and
"descent" concepts apply to both a component's horizontal size as well as its vertical size, although only one
(either vertical or horizontal) is used in a given BoxLayout. A component's horizontal ascent and descent
are used when it's added to a vertical BoxLayout, while its vertical ascent and descent are used when it is in
a horizontal BoxLayout.
If this seems somewhat confusing, keep in mind that the horizontal placement of components in a horizontal
box is simple they appear next to one another from left to right. Similarly, for a vertical box, components
are simply "stacked" from top to bottom. In either case, the alignment, ascent, and descent values are used to
calculate the component's position in the remaining dimension.
You can see an example of this behavior by compiling the following code, which uses a vertical BoxLayout:
import java.awt.*;
import javax.swing.*;
public class BoxTest {
public static void main(String[] args) {
JFrame f = new JFrame("Vertical BoxLayout-managed container");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = f.getContentPane();
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);
}
f.setSize(400, 300);
f.setVisible(true);
}
}
When executed, this code produces a display like the one shown below:
In addition to the alignment values assigned to each component, an alignment value is calculated for a container
when it's managed by a BoxLayout. The container's horizontal alignment is calculated by a vertical
BoxLayout, while the vertical alignment is used by a horizontal BoxLayout. These are accessible through
LayoutManager2's getLayoutAlignmentX() and getLayoutAlignmentY() methods, although
BoxLayout is currently the only layout manager that returns a meaningful value from those methods.
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.