APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class Box

extends JComponent
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Annotations
@JavaBean
defaultProperty:accessibleContext
@SuppressWarnings:serial
Imports
java.awt.*, java.beans.JavaBean, .BeanProperty, .ConstructorProperties, javax.accessibility.*

A lightweight container that uses a BoxLayout object as its layout manager. Box provides several class methods that are useful for containers using BoxLayout -- even non-Box containers.

The Box class can create several kinds of invisible components that affect layout: glue, struts, and rigid areas. If all the components your Box contains have a fixed size, you might want to use a glue component (returned by createGlue) to control the components' positions. If you need a fixed amount of space between two components, try using a strut (createHorizontalStrut or createVerticalStrut). If you need an invisible component that always takes up the same amount of space, get it by invoking createRigidArea.

If you are implementing a BoxLayout you can find further information and examples in 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
BoxLayout

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
Box.AccessibleBox

This class implements accessibility support for the Box class.

public static class
Box.Filler

An implementation of a lightweight component that participates in layout but has no view.

Field Summary

Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
Box(int axis)

Creates a Box that displays its components along the specified axis.

Method Summary

Modifier and TypeMethod and Description
public static Component

Returns:

the component
createGlue
()

Creates an invisible "glue" component that can be useful in a Box whose visible components have a maximum width (for a horizontal box) or height (for a vertical box).

public static Box

Returns:

the box
createHorizontalBox
()

Creates a Box that displays its components from left to right.

public static Component

Returns:

the component
createHorizontalGlue
()

Creates a horizontal glue component.

public static Component

Returns:

the component
createHorizontalStrut
(int
the width of the invisible component, in pixels >= 0
width
)

Creates an invisible, fixed-width component.

public static Component

Returns:

the component
createRigidArea
(Dimension
the dimensions of the invisible component
d
)

Creates an invisible component that's always the specified size.

public static Box

Returns:

the box
createVerticalBox
()

Creates a Box that displays its components from top to bottom.

public static Component

Returns:

the component
createVerticalGlue
()

Creates a vertical glue component.

public static Component

Returns:

the component
createVerticalStrut
(int
the height of the invisible component, in pixels >= 0
height
)

Creates an invisible, fixed-height component.

public AccessibleContext

Returns:

an AccessibleBox that serves as the AccessibleContext of this Box
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Box.

protected void
paintComponent(Graphics
the Graphics to paint to
g
)

Overrides javax.swing.JComponent.paintComponent.

Paints this Box.

public void
setLayout(LayoutManager
the layout manager to use
l
)

Overrides java.awt.Container.setLayout.

Throws an AWTError, since a Box can use only a BoxLayout.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetUIgetUIClassIDgetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenparamStringprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdateupdateUI

Constructor Detail

Boxback to summary
public Box(int axis)

Creates a Box that displays its components along the specified axis.

Parameters
axis:int

can be BoxLayout#X_AXIS, BoxLayout#Y_AXIS, BoxLayout#LINE_AXIS or BoxLayout#PAGE_AXIS.

Exceptions
AWTError:
if the axis is invalid
See Also
createHorizontalBox, createVerticalBox

Method Detail

createGlueback to summary
public static Component createGlue()

Creates an invisible "glue" component that can be useful in a Box whose visible components have a maximum width (for a horizontal box) or height (for a vertical box). You can think of the glue component as being a gooey substance that expands as much as necessary to fill the space between its neighboring components.

For example, suppose you have a horizontal box that contains two fixed-size components. If the box gets extra space, the fixed-size components won't become larger, so where does the extra space go? Without glue, the extra space goes to the right of the second component. If you put glue between the fixed-size components, then the extra space goes there. If you put glue before the first fixed-size component, the extra space goes there, and the fixed-size components are shoved against the right edge of the box. If you put glue before the first fixed-size component and after the second fixed-size component, the fixed-size components are centered in the box.

To use glue, call Box.createGlue and add the returned component to a container. The glue component has no minimum or preferred size, so it takes no space unless excess space is available. If excess space is available, then the glue component takes its share of available horizontal or vertical space, just like any other component that has no maximum width or height.

Returns:Component

the component

createHorizontalBoxback to summary
public static Box createHorizontalBox()

Creates a Box that displays its components from left to right. If you want a Box that respects the component orientation you should create the Box using the constructor and pass in BoxLayout.LINE_AXIS, eg:

  Box lineBox = new Box(BoxLayout.LINE_AXIS);
Returns:Box

the box

createHorizontalGlueback to summary
public static Component createHorizontalGlue()

Creates a horizontal glue component.

Returns:Component

the component

createHorizontalStrutback to summary
public static Component createHorizontalStrut(int width)

Creates an invisible, fixed-width component. In a horizontal box, you typically use this method to force a certain amount of space between two components. In a vertical box, you might use this method to force the box to be at least the specified width. The invisible component has no height unless excess space is available, in which case it takes its share of available space, just like any other component that has no maximum height.

Parameters
width:int

the width of the invisible component, in pixels >= 0

Returns:Component

the component

See Also
createVerticalStrut, createGlue, createRigidArea
createRigidAreaback to summary
public static Component createRigidArea(Dimension d)

Creates an invisible component that's always the specified size.

Parameters
d:Dimension

the dimensions of the invisible component

Returns:Component

the component

See Also
createGlue, createHorizontalStrut, createVerticalStrut
createVerticalBoxback to summary
public static Box createVerticalBox()

Creates a Box that displays its components from top to bottom. If you want a Box that respects the component orientation you should create the Box using the constructor and pass in BoxLayout.PAGE_AXIS, eg:

  Box lineBox = new Box(BoxLayout.PAGE_AXIS);
Returns:Box

the box

createVerticalGlueback to summary
public static Component createVerticalGlue()

Creates a vertical glue component.

Returns:Component

the component

createVerticalStrutback to summary
public static Component createVerticalStrut(int height)

Creates an invisible, fixed-height component. In a vertical box, you typically use this method to force a certain amount of space between two components. In a horizontal box, you might use this method to force the box to be at least the specified height. The invisible component has no width unless excess space is available, in which case it takes its share of available space, just like any other component that has no maximum width.

Parameters
height:int

the height of the invisible component, in pixels >= 0

Returns:Component

the component

See Also
createHorizontalStrut, createGlue, createRigidArea
getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Box. For boxes, the AccessibleContext takes the form of an AccessibleBox. A new AccessibleAWTBox instance is created if necessary.

Returns:AccessibleContext

an AccessibleBox that serves as the AccessibleContext of this Box

Annotations
@BeanProperty
bound:false
paintComponentback to summary
protected void paintComponent(Graphics g)

Overrides javax.swing.JComponent.paintComponent.

Paints this Box. If this Box has a UI this method invokes super's implementation, otherwise if this Box is opaque the Graphics is filled using the background.

Parameters
g:Graphics

the Graphics to paint to

Exceptions
NullPointerException:
if g is null
Since
1.6
setLayoutback to summary
public void setLayout(LayoutManager l)

Overrides java.awt.Container.setLayout.

Throws an AWTError, since a Box can use only a BoxLayout.

Parameters
l:LayoutManager

the layout manager to use

javax.swing back to summary

protected Class Box.AccessibleBox

extends AccessibleAWTContainer
Class Inheritance
Annotations
@SuppressWarnings:serial

This class implements accessibility support for the Box class.

Field Summary

Inherited from java.awt.Container.AccessibleAWTContainer:
accessibleContainerHandler

Constructor Summary

AccessConstructor and Description
protected
AccessibleBox()

Constructs an AccessibleBox.

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object (AccessibleRole.FILLER)
getAccessibleRole
()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Gets the role of this object.

Inherited from java.awt.Container.AccessibleAWTContainer:
addPropertyChangeListenergetAccessibleAtgetAccessibleChildgetAccessibleChildrenCountremovePropertyChangeListener

Constructor Detail

AccessibleBoxback to summary
protected AccessibleBox()

Constructs an AccessibleBox.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Gets the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object (AccessibleRole.FILLER)

See Also
AccessibleRole
javax.swing back to summary

public Class Box.Filler

extends JComponent
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Annotations
@SuppressWarnings:serial

An implementation of a lightweight component that participates in layout but has no view.

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
Box.Filler.AccessibleBoxFiller

This class implements accessibility support for the Box.Filler class.

Field Summary

Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
Filler(Dimension
Minimum size
min
,
Dimension
Preferred size
pref
,
Dimension
Maximum size
max
)

Constructor to create shape with the given size ranges.

Method Summary

Modifier and TypeMethod and Description
public void
changeShape(Dimension
Value to return for getMinimumSize
min
,
Dimension
Value to return for getPreferredSize
pref
,
Dimension
Value to return for getMaximumSize
max
)

Change the size requests for this shape.

public AccessibleContext

Returns:

an AccessibleBoxFiller that serves as the AccessibleContext of this Box.Filler.
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Box.Filler.

protected void
paintComponent(Graphics
the Graphics to paint to
g
)

Overrides javax.swing.JComponent.paintComponent.

Paints this Filler.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetUIgetUIClassIDgetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenparamStringprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdateupdateUI

Constructor Detail

Fillerback to summary
public Filler(Dimension min, Dimension pref, Dimension max)

Constructor to create shape with the given size ranges.

Parameters
min:Dimension

Minimum size

pref:Dimension

Preferred size

max:Dimension

Maximum size

Annotations
@ConstructorProperties:minimumSize, preferredSize, maximumSize

Method Detail

changeShapeback to summary
public void changeShape(Dimension min, Dimension pref, Dimension max)

Change the size requests for this shape. An invalidate() is propagated upward as a result so that layout will eventually happen with using the new sizes.

Parameters
min:Dimension

Value to return for getMinimumSize

pref:Dimension

Value to return for getPreferredSize

max:Dimension

Value to return for getMaximumSize

getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Box.Filler. For box fillers, the AccessibleContext takes the form of an AccessibleBoxFiller. A new AccessibleAWTBoxFiller instance is created if necessary.

Returns:AccessibleContext

an AccessibleBoxFiller that serves as the AccessibleContext of this Box.Filler.

paintComponentback to summary
protected void paintComponent(Graphics g)

Overrides javax.swing.JComponent.paintComponent.

Paints this Filler. If this Filler has a UI this method invokes super's implementation, otherwise if this Filler is opaque the Graphics is filled using the background.

Parameters
g:Graphics

the Graphics to paint to

Exceptions
NullPointerException:
if g is null
Since
1.6
javax.swing back to summary

protected Class Box.Filler.AccessibleBoxFiller

extends AccessibleAWTComponent
Class Inheritance
Annotations
@SuppressWarnings:serial

This class implements accessibility support for the Box.Filler class.

Constructor Summary

AccessConstructor and Description
protected
AccessibleBoxFiller()

Constructs an AccessibleBoxFiller.

Constructor Detail

AccessibleBoxFillerback to summary
protected AccessibleBoxFiller()

Constructs an AccessibleBoxFiller.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Gets the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object (AccessibleRole.FILLER)

See Also
AccessibleRole