javax.swing
Class BoxLayout

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by javax.swing.BoxLayout
All Implemented Interfaces:
LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager , LayoutManager2 sample code for java.awt.LayoutManager2 definition code for java.awt.LayoutManager2 , Serializable sample code for java.io.Serializable definition code for java.io.Serializable
Direct Known Subclasses:
DefaultMenuLayout sample code for javax.swing.plaf.basic.DefaultMenuLayout definition code for javax.swing.plaf.basic.DefaultMenuLayout

public class BoxLayout
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements LayoutManager2 sample code for java.awt.LayoutManager2 definition code for java.awt.LayoutManager2 , Serializable sample code for java.io.Serializable definition code for 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.
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 XMLEncoder sample code for java.beans.XMLEncoder definition code for java.beans.XMLEncoder .

See Also:
Box sample code for javax.swing.Box definition code for javax.swing.Box , ComponentOrientation sample code for java.awt.ComponentOrientation definition code for java.awt.ComponentOrientation , JComponent.getAlignmentX() sample code for javax.swing.JComponent.getAlignmentX() definition code for javax.swing.JComponent.getAlignmentX() , JComponent.getAlignmentY() sample code for javax.swing.JComponent.getAlignmentY() definition code for javax.swing.JComponent.getAlignmentY() , Serialized Form

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

Field Detail

X_AXIS sample code for javax.swing.BoxLayout.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 sample code for javax.swing.BoxLayout.Y_AXIS

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

See Also:
Constant Field Values

LINE_AXIS sample code for javax.swing.BoxLayout.LINE_AXIS

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.

See Also:
Constant Field Values

PAGE_AXIS sample code for javax.swing.BoxLayout.PAGE_AXIS

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.

See Also:
Constant Field Values
Constructor Detail

BoxLayout sample code for javax.swing.BoxLayout(java.awt.Container, int) definition code for javax.swing.BoxLayout(java.awt.Container, int)

public BoxLayout(Container sample code for java.awt.Container definition code for 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, BoxLayout.LINE_AXIS or BoxLayout.PAGE_AXIS
Throws:
AWTError sample code for java.awt.AWTError definition code for java.awt.AWTError - if the value of axis is invalid
Method Detail

invalidateLayout sample code for javax.swing.BoxLayout.invalidateLayout(java.awt.Container) definition code for javax.swing.BoxLayout.invalidateLayout(java.awt.Container)

public void invalidateLayout(Container sample code for java.awt.Container definition code for 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 sample code for java.awt.LayoutManager2.invalidateLayout(java.awt.Container) definition code for java.awt.LayoutManager2.invalidateLayout(java.awt.Container) in interface LayoutManager2 sample code for java.awt.LayoutManager2 definition code for java.awt.LayoutManager2
Parameters:
target - the affected container
Throws:
AWTError sample code for java.awt.AWTError definition code for java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

addLayoutComponent sample code for javax.swing.BoxLayout.addLayoutComponent(java.lang.String, java.awt.Component) definition code for javax.swing.BoxLayout.addLayoutComponent(java.lang.String, java.awt.Component)

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

Specified by:
addLayoutComponent sample code for java.awt.LayoutManager.addLayoutComponent(java.lang.String, java.awt.Component) definition code for java.awt.LayoutManager.addLayoutComponent(java.lang.String, java.awt.Component) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
name - the name of the component
comp - the component

removeLayoutComponent sample code for javax.swing.BoxLayout.removeLayoutComponent(java.awt.Component) definition code for javax.swing.BoxLayout.removeLayoutComponent(java.awt.Component)

public void removeLayoutComponent(Component sample code for java.awt.Component definition code for java.awt.Component  comp)
Not used by this class.

Specified by:
removeLayoutComponent sample code for java.awt.LayoutManager.removeLayoutComponent(java.awt.Component) definition code for java.awt.LayoutManager.removeLayoutComponent(java.awt.Component) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
comp - the component

addLayoutComponent sample code for javax.swing.BoxLayout.addLayoutComponent(java.awt.Component, java.lang.Object) definition code for javax.swing.BoxLayout.addLayoutComponent(java.awt.Component, java.lang.Object)

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

Specified by:
addLayoutComponent sample code for java.awt.LayoutManager2.addLayoutComponent(java.awt.Component, java.lang.Object) definition code for java.awt.LayoutManager2.addLayoutComponent(java.awt.Component, java.lang.Object) in interface LayoutManager2 sample code for java.awt.LayoutManager2 definition code for java.awt.LayoutManager2
Parameters:
comp - the component
constraints - constraints

preferredLayoutSize sample code for javax.swing.BoxLayout.preferredLayoutSize(java.awt.Container) definition code for javax.swing.BoxLayout.preferredLayoutSize(java.awt.Container)

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

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

minimumLayoutSize sample code for javax.swing.BoxLayout.minimumLayoutSize(java.awt.Container) definition code for javax.swing.BoxLayout.minimumLayoutSize(java.awt.Container)

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

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

maximumLayoutSize sample code for javax.swing.BoxLayout.maximumLayoutSize(java.awt.Container) definition code for javax.swing.BoxLayout.maximumLayoutSize(java.awt.Container)

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

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

getLayoutAlignmentX sample code for javax.swing.BoxLayout.getLayoutAlignmentX(java.awt.Container) definition code for javax.swing.BoxLayout.getLayoutAlignmentX(java.awt.Container)

public float getLayoutAlignmentX(Container sample code for java.awt.Container definition code for 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 sample code for java.awt.LayoutManager2.getLayoutAlignmentX(java.awt.Container) definition code for java.awt.LayoutManager2.getLayoutAlignmentX(java.awt.Container) in interface LayoutManager2 sample code for java.awt.LayoutManager2 definition code for java.awt.LayoutManager2
Parameters:
target - the container
Returns:
the alignment >= 0.0f && <= 1.0f
Throws:
AWTError sample code for java.awt.AWTError definition code for java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

getLayoutAlignmentY sample code for javax.swing.BoxLayout.getLayoutAlignmentY(java.awt.Container) definition code for javax.swing.BoxLayout.getLayoutAlignmentY(java.awt.Container)

public float getLayoutAlignmentY(Container sample code for java.awt.Container definition code for 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 sample code for java.awt.LayoutManager2.getLayoutAlignmentY(java.awt.Container) definition code for java.awt.LayoutManager2.getLayoutAlignmentY(java.awt.Container) in interface LayoutManager2 sample code for java.awt.LayoutManager2 definition code for java.awt.LayoutManager2
Parameters:
target - the container
Returns:
the alignment >= 0.0f && <= 1.0f
Throws:
AWTError sample code for java.awt.AWTError definition code for java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor

layoutContainer sample code for javax.swing.BoxLayout.layoutContainer(java.awt.Container) definition code for javax.swing.BoxLayout.layoutContainer(java.awt.Container)

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

Specified by:
layoutContainer sample code for java.awt.LayoutManager.layoutContainer(java.awt.Container) definition code for java.awt.LayoutManager.layoutContainer(java.awt.Container) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
target - the container to lay out
Throws:
AWTError sample code for java.awt.AWTError definition code for java.awt.AWTError - if the target isn't the container specified to the BoxLayout constructor