APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing

public Class BoxLayout

extends Object
implements LayoutManager2, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.awt.LayoutManager2, java.awt.LayoutManager
Known Direct Subclasses
javax.swing.plaf.basic.DefaultMenuLayout
Annotations
@SuppressWarnings:serial
Imports
java.awt.AWTError, .Component, .ComponentOrientation, .Container, .Dimension, .Insets, .LayoutManager2, java.beans.ConstructorProperties, java.io.PrintStream, .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.

Example:

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:

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 a horizontal layout) or heights (for a 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 the BoxLayout layout manager. 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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
Timothy Prinzing
Since
1.2
See Also
Box, java.awt.ComponentOrientation, JComponent#getAlignmentX, JComponent#getAlignmentY

Field Summary

Modifier and TypeField and Description
private int
private transient PrintStream
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.

private Container
public static final int
X_AXIS

Specifies that components should be laid out left to right.

private transient SizeRequirements[]
private transient SizeRequirements
public static final int
Y_AXIS

Specifies that components should be laid out top to bottom.

private transient SizeRequirements[]
private transient SizeRequirements

Constructor Summary

AccessConstructor and Description
public
BoxLayout(Container
the container that needs to be laid out
target
,
int
the axis to lay out components along. Can be one of: BoxLayout.X_AXIS, BoxLayout.Y_AXIS, BoxLayout.LINE_AXIS or BoxLayout.PAGE_AXIS
axis
)

Creates a layout manager that will lay out components along the given axis.

pack-priv
BoxLayout(Container
the container that needs to be laid out
target
,
int
the axis to lay out components along. Can be one of: BoxLayout.X_AXIS, BoxLayout.Y_AXIS, BoxLayout.LINE_AXIS or BoxLayout.PAGE_AXIS
axis
,
PrintStream
the stream to which debugging messages should be sent, null if none
dbg
)

Constructs a BoxLayout that produces debugging messages.

Method Summary

Modifier and TypeMethod and Description
public void
addLayoutComponent(String
the name of the component
name
,
Component
the component
comp
)

Implements java.awt.LayoutManager.addLayoutComponent.

Not used by this class.

public void
addLayoutComponent(Component
the component
comp
,
Object
constraints
constraints
)

Implements java.awt.LayoutManager2.addLayoutComponent.

Not used by this class.

pack-priv void
pack-priv void
public final int

Returns:

the axis that was used to lay out components
getAxis
()

Returns the axis that was used to lay out components.

public synchronized float

Returns:

the alignment >= 0.0f && <= 1.0f
getLayoutAlignmentX
(Container
the container
target
)

Implements java.awt.LayoutManager2.getLayoutAlignmentX.

Returns the alignment along the X axis for the container.

public synchronized float

Returns:

the alignment >= 0.0f && <= 1.0f
getLayoutAlignmentY
(Container
the container
target
)

Implements java.awt.LayoutManager2.getLayoutAlignmentY.

Returns the alignment along the Y axis for the container.

public final Container

Returns:

the container that uses this layout manager
getTarget
()

Returns the container that uses this layout manager.

public synchronized void
invalidateLayout(Container
the affected container
target
)

Implements java.awt.LayoutManager2.invalidateLayout.

Indicates that a child has changed its layout related information, and thus any cached calculations should be flushed.

public void
layoutContainer(Container
the container to lay out
target
)

Implements java.awt.LayoutManager.layoutContainer.

Called by the AWT when the specified container needs to be laid out.

public Dimension

Returns:

the dimensions >= 0 && <= Integer.MAX_VALUE
maximumLayoutSize
(Container
the container that needs to be laid out
target
)

Implements java.awt.LayoutManager2.maximumLayoutSize.

Returns the maximum dimensions the target container can use to lay out the components it contains.

public Dimension

Returns:

the dimensions >= 0 && <= Integer.MAX_VALUE
minimumLayoutSize
(Container
the container that needs to be laid out
target
)

Implements java.awt.LayoutManager.minimumLayoutSize.

Returns the minimum dimensions needed to lay out the components contained in the specified target container.

public Dimension

Returns:

the dimensions >= 0 && <= Integer.MAX_VALUE
preferredLayoutSize
(Container
the container that needs to be laid out
target
)

Implements java.awt.LayoutManager.preferredLayoutSize.

Returns the preferred dimensions for this layout, given the components in the specified target container.

public void
removeLayoutComponent(Component
the component
comp
)

Implements java.awt.LayoutManager.removeLayoutComponent.

Not used by this class.

private int

Returns:

the resolved axis
resolveAxis
(int
the axis to resolve
axis
,
ComponentOrientation
the ComponentOrientation to resolve against
o
)

Given one of the 4 axis values, resolve it to an absolute axis.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

axisback to summary
private int axis
dbgback to summary
private transient PrintStream dbg
LINE_AXISback to summary
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.

PAGE_AXISback to summary
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.

targetback to summary
private Container target
X_AXISback to summary
public static final int X_AXIS

Specifies that components should be laid out left to right.

xChildrenback to summary
private transient SizeRequirements[] xChildren
xTotalback to summary
private transient SizeRequirements xTotal
Y_AXISback to summary
public static final int Y_AXIS

Specifies that components should be laid out top to bottom.

yChildrenback to summary
private transient SizeRequirements[] yChildren
yTotalback to summary
private transient SizeRequirements yTotal

Constructor Detail

BoxLayoutback to summary
public BoxLayout(Container target, int axis)

Creates a layout manager that will lay out components along the given axis.

Parameters
target:Container

the container that needs to be laid out

axis:int

the axis to lay out components along. Can be one of: BoxLayout.X_AXIS, BoxLayout.Y_AXIS, BoxLayout.LINE_AXIS or BoxLayout.PAGE_AXIS

Annotations
@ConstructorProperties:target, axis
Exceptions
AWTError:
if the value of axis is invalid
BoxLayoutback to summary
pack-priv BoxLayout(Container target, int axis, PrintStream dbg)

Constructs a BoxLayout that produces debugging messages.

Parameters
target:Container

the container that needs to be laid out

axis:int

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:PrintStream

the stream to which debugging messages should be sent, null if none

Method Detail

addLayoutComponentback to summary
public void addLayoutComponent(String name, Component comp)

Implements java.awt.LayoutManager.addLayoutComponent.

Not used by this class.

Parameters
name:String

the name of the component

comp:Component

the component

addLayoutComponentback to summary
public void addLayoutComponent(Component comp, Object constraints)

Implements java.awt.LayoutManager2.addLayoutComponent.

Not used by this class.

Parameters
comp:Component

the component

constraints:Object

constraints

checkContainerback to summary
pack-priv void checkContainer(Container target)
checkRequestsback to summary
pack-priv void checkRequests()
getAxisback to summary
public final int getAxis()

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

Returns:int

the axis that was used to lay out components

Since
1.6
getLayoutAlignmentXback to summary
public synchronized float getLayoutAlignmentX(Container target)

Implements java.awt.LayoutManager2.getLayoutAlignmentX.

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.

Parameters
target:Container

the container

Returns:float

the alignment >= 0.0f && <= 1.0f

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
getLayoutAlignmentYback to summary
public synchronized float getLayoutAlignmentY(Container target)

Implements java.awt.LayoutManager2.getLayoutAlignmentY.

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.

Parameters
target:Container

the container

Returns:float

the alignment >= 0.0f && <= 1.0f

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
getTargetback to summary
public final Container getTarget()

Returns the container that uses this layout manager.

Returns:Container

the container that uses this layout manager

Since
1.6
invalidateLayoutback to summary
public synchronized void invalidateLayout(Container target)

Implements java.awt.LayoutManager2.invalidateLayout.

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.

Parameters
target:Container

the affected container

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
layoutContainerback to summary
public void layoutContainer(Container target)

Implements java.awt.LayoutManager.layoutContainer.

Called by the AWT when the specified container needs to be laid out.

Parameters
target:Container

the container to lay out

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
maximumLayoutSizeback to summary
public Dimension maximumLayoutSize(Container target)

Implements java.awt.LayoutManager2.maximumLayoutSize.

Returns the maximum dimensions the target container can use to lay out the components it contains.

Parameters
target:Container

the container that needs to be laid out

Returns:Dimension

the dimensions >= 0 && <= Integer.MAX_VALUE

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
See Also
preferredLayoutSize, minimumLayoutSize
minimumLayoutSizeback to summary
public Dimension minimumLayoutSize(Container target)

Implements java.awt.LayoutManager.minimumLayoutSize.

Returns the minimum dimensions needed to lay out the components contained in the specified target container.

Parameters
target:Container

the container that needs to be laid out

Returns:Dimension

the dimensions >= 0 && <= Integer.MAX_VALUE

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
See Also
preferredLayoutSize, maximumLayoutSize
preferredLayoutSizeback to summary
public Dimension preferredLayoutSize(Container target)

Implements java.awt.LayoutManager.preferredLayoutSize.

Returns the preferred dimensions for this layout, given the components in the specified target container.

Parameters
target:Container

the container that needs to be laid out

Returns:Dimension

the dimensions >= 0 && <= Integer.MAX_VALUE

Exceptions
AWTError:
if the target isn't the container specified to the BoxLayout constructor
See Also
Container, minimumLayoutSize, maximumLayoutSize
removeLayoutComponentback to summary
public void removeLayoutComponent(Component comp)

Implements java.awt.LayoutManager.removeLayoutComponent.

Not used by this class.

Parameters
comp:Component

the component

resolveAxisback to summary
private int resolveAxis(int axis, ComponentOrientation o)

Given one of the 4 axis values, resolve it to an absolute axis. The relative axis values, PAGE_AXIS and LINE_AXIS, are converted to their absolute counterpart given the target's ComponentOrientation value. The absolute axes, X_AXIS and Y_AXIS, are returned unmodified.

Parameters
axis:int

the axis to resolve

o:ComponentOrientation

the ComponentOrientation to resolve against

Returns:int

the resolved axis