javax.swing
public class: BoxLayout [javadoc |
source]
java.lang.Object
javax.swing.BoxLayout
All Implemented Interfaces:
LayoutManager2, Serializable
Direct Known Subclasses:
DefaultMenuLayout, DefaultMenuLayout
A layout manager that allows multiple components to be laid out either
vertically or horizontally. The components will not wrap so, for
example, a vertical arrangement of components will stay vertically
arranged when the frame is resized.
Nesting multiple panels with different combinations of horizontal and
vertical gives an effect similar to GridBagLayout, without the
complexity. The diagram shows two panels arranged horizontally, each
of which contains 3 components arranged vertically.
The BoxLayout manager is constructed with an axis parameter that
specifies the type of layout that will be done. There are four choices:
X_AXIS - Components are laid out horizontally
from left to right.
Y_AXIS - Components are laid out vertically
from top to bottom.
LINE_AXIS - Components are laid out the way
words are laid out in a line, based on the container's
ComponentOrientation property. If the container's
ComponentOrientation is horizontal then components are laid out
horizontally, otherwise they are laid out vertically. For horizontal
orientations, if the container's ComponentOrientation is left to
right then components are laid out left to right, otherwise they are laid
out right to left. For vertical orientations components are always laid out
from top to bottom.
PAGE_AXIS - Components are laid out the way
text lines are laid out on a page, based on the container's
ComponentOrientation property. If the container's
ComponentOrientation is horizontal then components are laid out
vertically, otherwise they are laid out horizontally. For horizontal
orientations, if the container's ComponentOrientation is left to
right then components are laid out left to right, otherwise they are laid
out right to left. For vertical orientations components are always
laid out from top to bottom.
For all directions, components are arranged in the same order as they were
added to the container.
BoxLayout attempts to arrange components
at their preferred widths (for horizontal layout)
or heights (for vertical layout).
For a horizontal layout,
if not all the components are the same height,
BoxLayout attempts to make all the components
as high as the highest component.
If that's not possible for a particular component,
then BoxLayout aligns that component vertically,
according to the component's Y alignment.
By default, a component has a Y alignment of 0.5,
which means that the vertical center of the component
should have the same Y coordinate as
the vertical centers of other components with 0.5 Y alignment.
Similarly, for a vertical layout,
BoxLayout attempts to make all components in the column
as wide as the widest component.
If that fails, it aligns them horizontally
according to their X alignments. For PAGE_AXIS layout,
horizontal alignment is done based on the leading edge of the component.
In other words, an X alignment value of 0.0 means the left edge of a
component if the container's ComponentOrientation is left to
right and it means the right edge of the component otherwise.
Instead of using BoxLayout directly, many programs use the Box class.
The Box class is a lightweight container that uses a BoxLayout.
It also provides handy methods to help you use BoxLayout well.
Adding components to multiple nested boxes is a powerful way to get
the arrangement you want.
For further information and examples see
How to Use BoxLayout,
a section in The Java Tutorial.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see java.beans.XMLEncoder .
| Field Summary |
|---|
| public static final int | X_AXIS | Specifies that components should be laid out left to right. |
| public static final int | Y_AXIS | Specifies that components should be laid out top to bottom. |
| public static final int | LINE_AXIS | Specifies that components should be laid out in the direction of
a line of text as determined by the target container's
ComponentOrientation property. |
| public static final int | PAGE_AXIS | Specifies that components should be laid out in the direction that
lines flow across a page as determined by the target container's
ComponentOrientation property. |
| Constructor: |
public BoxLayout(Container target,
int axis) {
if (axis != X_AXIS && axis != Y_AXIS &&
axis != LINE_AXIS && axis != PAGE_AXIS) {
throw new AWTError("Invalid axis");
}
this.axis = axis;
this.target = target;
}
Creates a layout manager that will lay out components along the
given axis. Parameters:
target - the container that needs to be laid out
axis - the axis to lay out components along. Can be one of:
BoxLayout.X_AXIS,
BoxLayout.Y_AXIS,
BoxLayout.LINE_AXIS or
BoxLayout.PAGE_AXIS
Throws:
AWTError - if the value of axis is invalid
- exception:
AWTError - if the value of axis is invalid
|
BoxLayout(Container target,
int axis,
PrintStream dbg) {
this(target, axis);
this.dbg = dbg;
}
Constructs a BoxLayout that
produces debugging messages. Parameters:
target - the container that needs to be laid out
axis - the axis to lay out components along. Can be one of:
BoxLayout.X_AXIS,
BoxLayout.Y_AXIS,
BoxLayout.LINE_AXIS or
BoxLayout.PAGE_AXIS
dbg - the stream to which debugging messages should be sent,
null if none
|
| Method from javax.swing.BoxLayout Summary: |
|---|
|
addLayoutComponent, addLayoutComponent, checkContainer, checkRequests, getAxis, getLayoutAlignmentX, getLayoutAlignmentY, getTarget, invalidateLayout, layoutContainer, maximumLayoutSize, minimumLayoutSize, preferredLayoutSize, removeLayoutComponent |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from javax.swing.BoxLayout Detail: |
public void addLayoutComponent(String name,
Component comp) {
invalidateLayout(comp.getParent());
}
|
public void addLayoutComponent(Component comp,
Object constraints) {
invalidateLayout(comp.getParent());
}
|
void checkContainer(Container target) {
if (this.target != target) {
throw new AWTError("BoxLayout can't be shared");
}
}
|
void checkRequests() {
if (xChildren == null || yChildren == null) {
// The requests have been invalidated... recalculate
// the request information.
int n = target.getComponentCount();
xChildren = new SizeRequirements[n];
yChildren = new SizeRequirements[n];
for (int i = 0; i < n; i++) {
Component c = target.getComponent(i);
if (!c.isVisible()) {
xChildren[i] = new SizeRequirements(0,0,0, c.getAlignmentX());
yChildren[i] = new SizeRequirements(0,0,0, c.getAlignmentY());
continue;
}
Dimension min = c.getMinimumSize();
Dimension typ = c.getPreferredSize();
Dimension max = c.getMaximumSize();
xChildren[i] = new SizeRequirements(min.width, typ.width,
max.width,
c.getAlignmentX());
yChildren[i] = new SizeRequirements(min.height, typ.height,
max.height,
c.getAlignmentY());
}
// Resolve axis to an absolute value (either X_AXIS or Y_AXIS)
int absoluteAxis = resolveAxis(axis,target.getComponentOrientation());
if (absoluteAxis == X_AXIS) {
xTotal = SizeRequirements.getTiledSizeRequirements(xChildren);
yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
} else {
xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
yTotal = SizeRequirements.getTiledSizeRequirements(yChildren);
}
}
}
|
public final int getAxis() {
return this.axis;
}
Returns the axis that was used to lay out components.
Returns one of:
BoxLayout.X_AXIS,
BoxLayout.Y_AXIS,
BoxLayout.LINE_AXIS or
BoxLayout.PAGE_AXIS |
public synchronized float getLayoutAlignmentX(Container target) {
checkContainer(target);
checkRequests();
return xTotal.alignment;
}
Returns the alignment along the X axis for the container.
If the box is horizontal, the default
alignment will be returned. Otherwise, the alignment needed
to place the children along the X axis will be returned. |
public synchronized float getLayoutAlignmentY(Container target) {
checkContainer(target);
checkRequests();
return yTotal.alignment;
}
Returns the alignment along the Y axis for the container.
If the box is vertical, the default
alignment will be returned. Otherwise, the alignment needed
to place the children along the Y axis will be returned. |
public final Container getTarget() {
return this.target;
}
Returns the container that uses this layout manager. |
public synchronized void invalidateLayout(Container target) {
checkContainer(target);
xChildren = null;
yChildren = null;
xTotal = null;
yTotal = null;
}
Indicates that a child has changed its layout related information,
and thus any cached calculations should be flushed.
This method is called by AWT when the invalidate method is called
on the Container. Since the invalidate method may be called
asynchronously to the event thread, this method may be called
asynchronously. |
public void layoutContainer(Container target) {
checkContainer(target);
int nChildren = target.getComponentCount();
int[] xOffsets = new int[nChildren];
int[] xSpans = new int[nChildren];
int[] yOffsets = new int[nChildren];
int[] ySpans = new int[nChildren];
Dimension alloc = target.getSize();
Insets in = target.getInsets();
alloc.width -= in.left + in.right;
alloc.height -= in.top + in.bottom;
// Resolve axis to an absolute value (either X_AXIS or Y_AXIS)
ComponentOrientation o = target.getComponentOrientation();
int absoluteAxis = resolveAxis( axis, o );
boolean ltr = (absoluteAxis != axis) ? o.isLeftToRight() : true;
// determine the child placements
synchronized(this) {
checkRequests();
if (absoluteAxis == X_AXIS) {
SizeRequirements.calculateTiledPositions(alloc.width, xTotal,
xChildren, xOffsets,
xSpans, ltr);
SizeRequirements.calculateAlignedPositions(alloc.height, yTotal,
yChildren, yOffsets,
ySpans);
} else {
SizeRequirements.calculateAlignedPositions(alloc.width, xTotal,
xChildren, xOffsets,
xSpans, ltr);
SizeRequirements.calculateTiledPositions(alloc.height, yTotal,
yChildren, yOffsets,
ySpans);
}
}
// flush changes to the container
for (int i = 0; i < nChildren; i++) {
Component c = target.getComponent(i);
c.setBounds((int) Math.min((long) in.left + (long) xOffsets[i], Integer.MAX_VALUE),
(int) Math.min((long) in.top + (long) yOffsets[i], Integer.MAX_VALUE),
xSpans[i], ySpans[i]);
}
if (dbg != null) {
for (int i = 0; i < nChildren; i++) {
Component c = target.getComponent(i);
dbg.println(c.toString());
dbg.println("X: " + xChildren[i]);
dbg.println("Y: " + yChildren[i]);
}
}
}
Called by the AWT when the specified container
needs to be laid out. |
public Dimension maximumLayoutSize(Container target) {
Dimension size;
synchronized(this) {
checkContainer(target);
checkRequests();
size = new Dimension(xTotal.maximum, yTotal.maximum);
}
Insets insets = target.getInsets();
size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
return size;
}
Returns the maximum dimensions the target container can use
to lay out the components it contains. |
public Dimension minimumLayoutSize(Container target) {
Dimension size;
synchronized(this) {
checkContainer(target);
checkRequests();
size = new Dimension(xTotal.minimum, yTotal.minimum);
}
Insets insets = target.getInsets();
size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
return size;
}
Returns the minimum dimensions needed to lay out the components
contained in the specified target container. |
public Dimension preferredLayoutSize(Container target) {
Dimension size;
synchronized(this) {
checkContainer(target);
checkRequests();
size = new Dimension(xTotal.preferred, yTotal.preferred);
}
Insets insets = target.getInsets();
size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
return size;
}
Returns the preferred dimensions for this layout, given the components
in the specified target container. |
public void removeLayoutComponent(Component comp) {
invalidateLayout(comp.getParent());
}
|