JSR-209 (Final Release)

javax.swing
Class BoxLayout

java.lang.Object
  extended byjavax.swing.BoxLayout
All Implemented Interfaces:
java.awt.LayoutManager, java.awt.LayoutManager2, java.io.Serializable

public class BoxLayout
extends java.lang.Object
implements java.awt.LayoutManager2, java.io.Serializable

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.

The following text describes this graphic.

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.

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.

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.

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.

See Also:
Box, JComponent.getAlignmentX(), JComponent.getAlignmentY()

Field Summary
static int X_AXIS
          Specifies that components should be laid out left to right.
static int Y_AXIS
          Specifies that components should be laid out top to bottom.
 
Constructor Summary
BoxLayout(java.awt.Container target, int axis)
          Creates a layout manager that will lay out components along the given axis.
 
Method Summary
 void addLayoutComponent(java.awt.Component comp, java.lang.Object constraints)
          Not used by this class.
 void addLayoutComponent(java.lang.String name, java.awt.Component comp)
          Not used by this class.
 float getLayoutAlignmentX(java.awt.Container target)
          Returns the alignment along the X axis for the container.
 float getLayoutAlignmentY(java.awt.Container target)
          Returns the alignment along the Y axis for the container.
 void invalidateLayout(java.awt.Container target)
          Indicates that a child has changed its layout related information, and thus any cached calculations should be flushed.
 void layoutContainer(java.awt.Container target)
          Called by the AWT when the specified container needs to be laid out.
 Dimension maximumLayoutSize(java.awt.Container target)
          Returns the maximum dimensions the target container can use to lay out the components it contains.
 Dimension minimumLayoutSize(java.awt.Container target)
          Returns the minimum dimensions needed to lay out the components contained in the specified target container.
 Dimension preferredLayoutSize(java.awt.Container target)
          Returns the preferred dimensions for this layout, given the components in the specified target container.
 void removeLayoutComponent(java.awt.Component comp)
          Not used by this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

X_AXIS

public static final int X_AXIS
Specifies that components should be laid out left to right.

See Also:
Constant Field Values

Y_AXIS

public static final int Y_AXIS
Specifies that components should be laid out top to bottom.

See Also:
Constant Field Values
Constructor Detail

BoxLayout

public BoxLayout(java.awt.Container target,
                 int axis)
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
Throws:
java.awt.AWTError - if the value of axis is invalid
Method Detail

invalidateLayout

public void invalidateLayout(java.awt.Container target)
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.

Specified by:
invalidateLayout in interface java.awt.LayoutManager2
Parameters:
target - the affected container
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

addLayoutComponent

public void addLayoutComponent(java.lang.String name,
                               java.awt.Component comp)
Not used by this class.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager
Parameters:
name - the name of the component
comp - the component

removeLayoutComponent

public void removeLayoutComponent(java.awt.Component comp)
Not used by this class.

Specified by:
removeLayoutComponent in interface java.awt.LayoutManager
Parameters:
comp - the component

addLayoutComponent

public void addLayoutComponent(java.awt.Component comp,
                               java.lang.Object constraints)
Not used by this class.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager2
Parameters:
comp - the component
constraints - constraints

preferredLayoutSize

public Dimension preferredLayoutSize(java.awt.Container target)
Returns the preferred dimensions for this layout, given the components in the specified target container.

Specified by:
preferredLayoutSize in interface java.awt.LayoutManager
Parameters:
target - the container that needs to be laid out
Returns:
the dimensions >= 0 && <= Integer.MAX_VALUE
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor
See Also:
Container, minimumLayoutSize(java.awt.Container), maximumLayoutSize(java.awt.Container)

minimumLayoutSize

public Dimension minimumLayoutSize(java.awt.Container target)
Returns the minimum dimensions needed to lay out the components contained in the specified target container.

Specified by:
minimumLayoutSize in interface java.awt.LayoutManager
Parameters:
target - the container that needs to be laid out
Returns:
the dimensions >= 0 && <= Integer.MAX_VALUE
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor
See Also:
preferredLayoutSize(java.awt.Container), maximumLayoutSize(java.awt.Container)

maximumLayoutSize

public Dimension maximumLayoutSize(java.awt.Container target)
Returns the maximum dimensions the target container can use to lay out the components it contains.

Specified by:
maximumLayoutSize in interface java.awt.LayoutManager2
Parameters:
target - the container that needs to be laid out
Returns:
the dimenions >= 0 && <= Integer.MAX_VALUE
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor
See Also:
preferredLayoutSize(java.awt.Container), minimumLayoutSize(java.awt.Container)

getLayoutAlignmentX

public float getLayoutAlignmentX(java.awt.Container target)
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.

Specified by:
getLayoutAlignmentX in interface java.awt.LayoutManager2
Parameters:
target - the container
Returns:
the alignment >= 0.0f && <= 1.0f
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

getLayoutAlignmentY

public float getLayoutAlignmentY(java.awt.Container target)
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.

Specified by:
getLayoutAlignmentY in interface java.awt.LayoutManager2
Parameters:
target - the container
Returns:
the alignment >= 0.0f && <= 1.0f
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

layoutContainer

public void layoutContainer(java.awt.Container target)
Called by the AWT when the specified container needs to be laid out.

Specified by:
layoutContainer in interface java.awt.LayoutManager
Parameters:
target - the container to lay out
Throws:
java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

JSR-209 (Final Release)

Java and Java 2D are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Copyright 1993 - 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All Rights Reserved.
Use of this specification is subject to this license.