You are on page 1of 9

javax.swing.

Box

A Box object is a GUI container that uses the box layout manager. You can easily build complex
arrangements of GUI components by placing them within multiple nested boxes.

Although javax.swing.Box has a constructor allowing you to build Box objects, these two
static methods are easier to use:

javax.swing.Box Methods for Creating Boxes


static Box createHorizontalBox( )
static Box createVerticalBox( )

Box also declares static methods that create invisible GUI components that you can use to
improve the layout of your GUIs. You can even use these in GUI containers that aren’t Box
objects (such as JPanel objects). Don’t forget to import all these static methods.

Example
Assume that an application has built a window with the following code:

JFrame win = new JFrame( "Box Demo" );


win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

The code below builds two labels colored red and blue.

JLabel label1 = new JLabel( "A LABEL" );


label1.setOpaque( true );
label1.setBackground( Color.RED );
label1.setForeground( Color.WHITE );
JLabel label2 = new JLabel( "ANOTHER LABEL" );
label2.setOpaque( true );
label2.setBackground( Color.BLUE );
label2.setForeground( Color.WHITE );

The code below creates a horizontally-oriented box and adds the labels to it.

Box box = createHorizontalBox( );


box.add( label1 );
box.add( label2 );

javax.swing.Box Page 1
Once the box is added to the containment hierarchy, the
resulting window appears as shown to the right.

Example
With all other code the same as the previous example, this
code creates a vertically-oriented box for the labels.

Box box = createVerticalBox( );


box.add( label1 );
box.add( label2 );

Aligning Components within Boxes


You can separately control the alignment of each GUI component within a box (or other GUI
containers) by calling the following get and set methods, which the component inherits from the
Component class.

javax.swing.Component Methods for Controlling Alignment


float getAlignmentX( )
float getAlignmentY( )
void setAlignmentX( float setting )
void setAlignmentY( float setting )

X refers to the component’s horizontal alignment within the box; Y refers to its vertical
alignment. The alignment of the component is relative to the center of the container rather than
to its left or right sides. The alignment setting is a floating-point value. Class Component
provides the following ease-of-use constants for this setting.

Alignment Constants for Subclasses of Component


BOTTOM_ALIGNMENT Align the bottom of the GUI component to the center of the container
CENTER_ALIGNMENT Align the center of the GUI component to the center of the container

javax.swing.Box Page 2
LEFT_ALIGNMENT Align the left of the GUI component to the center of the container
RIGHT_ALIGNMENT Align the center of the GUI component to the center of the container
TOP_ALIGNMENT Align the top of the GUI component to the center of the container

Example
A GUI with top, bottom and center aligned components.

Box box = createHorizontalBox( );


box.add( label1 );
label1.setAlignmentY( TOP_ALIGNMENT );
box.add( label2 );
label2.setAlignmentY( BOTTOM_ALIGNMENT );
box.add( label3 );
label3.setAlignmentY( CENTER_ALIGNMENT );

Example
A GUI with left, right and center aligned components.

Box box = createVerticalBox( );


box.add( label1 );
label1.setAlignmentX( LEFT_ALIGNMENT );
box.add( label2 );
label2.setAlignmentX( RIGHT_ALIGNMENT );
box.add( label3 );
label3.setAlignmentX( CENTER_ALIGNMENT );

javax.swing.Box Page 3
Struts
A strut is an invisible GUI component that has a fixed size in pixels. The following static
methods create struts; add them to the container as you would any other GUI component.

javax.swing.Box Methods for Creating Struts


static Component createHorizontalStrut( int width )
// Create an invisible component 'width' pixels wide and no
// height.
static Component createVerticalStrut( int height )
// Create an invisible component 'height' pixels wide and no
// width.

Example
A horizontal strut used to separate components horizontally
by a fixed space.

Box box = createHorizontalBox( );


box.add( label1 );
box.add( createHorizontalStrut( 10 ) );
box.add( label2 );

Example
A horizontal strut used to maintain a minimum width within
a vertical box.

Box box = createVerticalBox( );


box.setBorder( createLineBorder( Color.BLACK, 2 ) );
box.add( createHorizontalStrut( 250 ) );
box.add( label1 );
box.add( label2 );

javax.swing.Box Page 4
Example
A vertical strut used to separate components vertically by a
fixed space

Box box = createVerticalBox( );


box.add( label1 );
box.add( createVerticalStrut( 10 ) );
box.add( label2 );

Example
A vertical strut used to maintain a minimum height within a
horizontal box.

Box box = createHorizontalBox( );


box.setBorder( createLineBorder( Color.BLACK, 2 ) );
box.add( createVerticalStrut( 50 ) );
box.add( label1 );
box.add( label2 );

javax.swing.Box Page 5
Glue
Glue is an invisible GUI component that expands or contracts in size as needed to fill up the
space between components. It is useful for keeping components oriented within a container as it
is resized. Glue is created with this static method:

static Component createGlue( )

Examples
This GUI has no glue components.

This GUI has glue to the left of the red label.

This GUI has glue to the left of the red label and to the right of the blue label.

Example
The third GUI above is created by the following code.

box.add( createGlue( ) );
box.add( label1 );
box.add( createHorizontalStrut( 10 ) );
box.add( label2 );
box.add( createGlue( ) );

javax.swing.Box Page 6
Rigid Areas
A rigid area is an invisible GUI component with a fixed width and height; in other words, a strut
with two dimensions. Here is the static method to create one:

static Component createRigidArea( Dimension d )

The parameter is a Dimension object that specifies the width and height of the rigid area.

Example
These two labels are separated by rigid area 10
pixels high and 250 pixels wide.

Box box = createVerticalBox( );


box.setBorder( createLineBorder( Color.BLACK, 2 ) );
box.add( label1 );
box.add( createRigidArea( new Dimension( 250, 10 ) ) );
box.add( label2 );

javax.swing.Box Page 7
For further information on using boxes and the box layout manager, see How to Use BoxLayout
in The Java™ Tutorials (google java tutorial boxlayout).

Exercises

Using the code from the examples, write complete Java programs (applications or applets) that
use a Box object to build and display the following GUIs.

1.

2.

3.

4.

5.

javax.swing.Box Page 8
6. Write a complete Java program that builds
this GUI.

You must put the three components


into a vertical box separated by
struts.

7. Write a complete Java program that builds


this GUI.

The two components on each row


must be put into a horizontal box
separated by a strut of at least 5
points.
The three horizontal boxes must be
placed into a vertical box separated
by struts of at least 10 points.
You may need to add glue to the
middle box so that the buttons are
aligned as shown.

javax.swing.Box Page 9

You might also like