Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Free CD from Sqribe Technologies, Click Here
Free CD from Sqribe Technologies, Click Here
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Invisible Components

The BoxLayout layout manager class places components next to each other in the container being managed. This often causes the container to appear cramped. This is shown in Figure 13.3, where the labels are placed right on top of each other. An empty border could be set on the labels to space the container, or invisible components could be added to the container. The Box class contains static methods that return invisible components, which can be used to help arrange the components in a container. Three types of invisible components are available: rigid, glue, and strut. However, JavaSoft is discouraging the use of struts with the BoxLayout layout manager. From the JavaSoft documentation:

The Box class provides another kind of filler for putting fixed space between components: a vertical or horizontal strut. Unfortunately, struts have unlimited maximum heights and widths (for horizontal and vertical struts, respectively). This means that if you use a horizontal box within a vertical box, the horizontal box can sometimes become too tall. For this reason, we recommend that you use rigid areas instead of struts.

The following two sections give example usage of the rigid and glue invisible components. The strut component is not discussed here. The methods that create these components are static, so they can be used to add invisible components to any container, not just instances of the Box class.

rigid Components

A rigid component takes up a defined amount of space and is not resizable. The static createRigidArea method in the Box class takes a Dimension parameter and returns an AWT component the size of the given dimension. Space can be added between the labels from the example application, shown in Figure 13.3, by adding rigid components to the container. The following change to the code, which creates the labels and adds them to the container, demonstrates this technique:

//
// Add three labels to the outer panel.
// Give each label a different background color
// and make it opaque to see the color.
//
// Use rigid areas to add some space between the labels.
//
panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

JLabel label = new JLabel( “Label 1” );
label.setOpaque( true );
label.setBackground( Color.yellow );
panel.add( label );

panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

label = new JLabel( “Label 2” );
label.setOpaque( true );
label.setBackground( Color.blue );
panel.add( label );

panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

label = new JLabel( “Label 3” );
label.setOpaque( true );
label.setBackground( Color.green );
panel.add( label );

panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

The resulting container is shown in Figure 13.5.


Figure 13.5  rigid components added to a BoxLayout.

glue Components

The example in the previous section used rigid components to add space between the labels in the container. This added a fixed area between the labels that did not change. If the container is resized, this may not produce the desired results. For example, Figure 13.6 shows the previous example after it has been expanded. All the extra space from the layout is added to the bottom of the container, and the labels remain fixed at the top of the container.


Figure 13.6  Expanded rigid components in the BoxLayout example.

An invisible glue component will stretch to any size required by the layout manager. In the previous example, if the labels were to remain vertically centered in the container, the first and last rigid components could be changed to glue. However, a glue component’s preferred dimensions are zero width and height. Replacing the rigid component would remove the space on top of the first label and below the bottom label. This is shown in Figure 13.7. If the initial space above and below the labels is desired, the glue can be added along with the rigid components. When the container is resized, the labels remain centered in the container. This code is shown below, and the resulting container is shown in Figure 13.8.

panel.add( Box.createGlue() );
panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

JLabel label = new JLabel( “Label 1” );
label.setOpaque( true );
label.setBackground( Color.yellow );
panel.add( label );

panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

label = new JLabel( “Label 2” );
label.setOpaque( true );
label.setBackground( Color.blue );
panel.add( label );

panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );

label = new JLabel( “Label 3” );
label.setOpaque( true );
label.setBackground( Color.green );
panel.add( label );

panel.add( Box.createRigidArea( new Dimension( 1, 4 ) ) );
panel.add( Box.createGlue() );


Figure 13.7  glue augmenting top and bottom rigid components in a BoxLayout.


Figure 13.8  Top and bottom glue components in addition to rigid components in a BoxLayout.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.