Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click here for Oracle 8i Internet Seminars.
Click here for Oracle 8i Internet Seminars.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


A glue component could be placed between each label. This would force the BoxLayout layout manager to evenly distribute extra space between each label in the container. This code is shown below, and the resulting container is shown in Figure 13.9, both before and after resizing the frame. As shown in the figure, the rigid components add spacing to the initial layout, and the glue components force the layout manager to evenly distribute the labels when the panel is larger than its preferred size.

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 ) ) );
panel.add( Box.createGlue() );

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

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

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.9  glue components in addition to rigid components in a BoxLayout.

The methods createVerticalGlue and createHorizontalGlue in the Box class create invisible glue components that only expand in one direction.

Static Configuration Methods

The Box class contains two static methods that return a Box configured with a BoxLayout along the implied axis. These methods, createHorizontalBox and createVerticalBox, configure the BoxLayout layout manager along the x-axis and y-axis, respectively. Using one of these methods eliminates the need to call the Box constructor with the axis parameter.

The JBox Class

In the previous section, the Box class was presented. It was pointed out that the Box class doesn’t extend the JComponent class. As such, it does not inherit the properties or functionality of that class. You also saw how the BoxLayout class provides simple layout management that is sufficient for many containers. Creating your own JBox class can eliminate the shortcomings of the Box class.

The JBox class shown in Listing 13.3 is a replacement for the Box class, which is a descendant of the JComponent class. Because the methods that create invisible components in the Box class are static, they can be used to create invisible components for the JBox class:

Listing 13.3 The JBox Class

package com.foley.utility;

import javax.swing.*;

/**
 * The JBox class is a Box replacement that
 * is a descendent of the JComponent class.
 * It is a JPanel configured with a BoxLayout
 * layout manager.
 * <p>
 * @author Mike Foley
 **/
public class JBox extends JPanel {

    public JBox( int orientation ) {
        BoxLayout boxLayout = new BoxLayout( this, orientation );
        setLayout( boxLayout );
    }

    /**
     * Create a JBox configured along its X-axis.
     * <p>
     * @return A JBox configured along its X-axis.
     **/
    public static JBox createHorizontalJBox() {
        return new JBox( BoxLayout.X_AXIS );
    }

    /**
     * Create a JBox configured along its Y-axis.
     * <p>
     * @return A JBox configured along its Y-axis.
     **/
    public static JBox createVerticalJBox() {
        return new JBox( BoxLayout.X_AXIS );
    }

} // JBox

Using the JBox Class

The JBox class is an API-compatible replacement for the Box class contained in the JFC. The static, invisible component-creation methods are not duplicated in the JBox class. These methods are static in the Box class, so they can be used with the JBox class, or any other class for that matter. The primary reason for using the JBox class instead of the Box class is to enable the functionality inherited from the JComponent class. For example, a border can be set on a JBox instance, but not on a Box instance.


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.