Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
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


Listing 13.1 The BoxLayoutTest Application

package com.foley.test;

import java.awt.*;

import javax.swing.*;

import com.foley.utility.ApplicationFrame;

/**
 * An application that displays panels in a
 * panel configured with a BoxLayout layout manager.
 *
 * @author Mike Foley
 **/
public class BoxLayoutTestextends Object {

    /**
     * Application entry point.
     * Create a panel with a BoxLayout layout manager.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        JFrame frame = new ApplicationFrame( “ BoxLayoutTest” );

        JPanel panel = new JPanel();
        panel.setBorder( BorderFactory.createLoweredBevelBorder() );
        BoxLayout boxLayout = new BoxLayout( panel, BoxLayout.Y_AXIS );
        panel.setLayout( boxLayout );

        //
        // Add three panels to the outer panel.
        // Give each panel a different background color to
        // distinguish them in the window.
        //
        JPanel p = new JPanel();
        p.setBackground( Color.yellow );
        panel.add( p );

        p = new JPanel();
        p.setBackground( Color.blue );
        panel.add( p );

        p = new JPanel();
        p.setBackground( Color.green );
        panel.add( p );

        frame.getContentPane().add( panel, BorderLayout.CENTER );
        frame.pack();
        frame.setVisible( true );

    } // main

} // BoxLayoutTest


Figure 13.1  The BoxLayout manager.

The BoxLayout class respects the minimum and maximum sizes of components while laying out a container. You can verify this by changing the preceding example to set minimum and maximum sizes for the three panels managed by the BoxLayout layout manager, as shown here:

JPanel p = new JPanel();
p.setBackground( Color.yellow );
p.setMinimumSize( new Dimension( 200, 10 ) );
p.setMaximumSize( new Dimension( 200, 10 ) );
panel.add( p );

p = new JPanel();
p.setBackground( Color.blue );
p.setMaximumSize( new Dimension( 100, 5 ) );
panel.add( p );

p = new JPanel();
p.setMinimumSize( new Dimension( 300, 20 ) );
p.setMaximumSize( new Dimension( 400, 30 ) );
p.setBackground( Color.green );
panel.add( p );

The resulting panel, after the window has been expanded, is shown in Figure 13.2. As you can see, the maximum size for each of the color panels has not been exceeded.


Figure 13.2  Minimum and maximum sizes in the BoxLayout manager.

In Figure 13.2, each colored panel is centered in the center panel. The BoxLayout layout manager uses the alignmentX or alignmentY property contained in the AWT Component class to determine where components are located when they don’t fill the width of the container for vertical layouts, or the height for horizontal layouts. Each of the alignment properties is a float value that ranges from 0.0 to 1.0. The value 0.0 represents left alignment for vertical layouts and top alignment for horizontal layouts. Similarly, 1.0 represents right alignment for vertical layouts and bottom alignment for horizontal layouts. A value of 0.5 is center alignment for both types of layouts. The constants LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT, BOTTOM_ALIGNMENT, and CENTER_ALIGNMENT are defined in the AWT Component class for these common placements.

The JPanel class contains default values for the alignmentX and alignmentY properties of 0.5, which explains why the panels are centered in Figure 13.2. However, other classes may have different default values for these properties. For example, the JLabel class has the alignmentX property set to Component.LEFT_ALIGNMENT. You can verify this by changing the preceding example to create JLabel instances instead of JPanel instances. This is shown below. The opaque property is set to true for each label. This wasn’t required for the panels because, as you saw in the previous section, panels are opaque by default:

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

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

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

The resulting window is shown in Figure 13.3. Note that the JLabel class returns its preferred size as its minimum and maximum sizes. Once again, other classes may behave differently.


Figure 13.3  JLabel instances in the BoxLayout layout manager.

Understanding how the alignmentX and alignmentY properties are used by the BoxLayout layout manager is difficult without practice. Listing 13.2 contains an application that dynamically alters these properties to show how a BoxLayout layout manager uses them when laying out a panel. This application contains three sliders that alter the alignmentX or alignmentY property of the three JLabel instances in its left panel. The application, shown in Figure 13.4, takes a single parameter that specifies the axis by which to configure the BoxLayout layout manager. Passing the application horizontal configures the layout manager along the x-axis. Specifying vertical or no parameter configures the layout manager for the y-axis. Moving the sliders allows you to see how the properties are used by the layout manager.


Figure 13.4  Horizontal and verticalBoxLayoutTest application.

The BoxLayoutTest panel itself uses a BoxLayout layout manager to configure the label and slider panel. The glue component added in the constructor will be discussed in the next section. It is also educational to replace the labels with other types of components to see their default alignment parameters. The BoxLayoutTest application demonstrates how relatively complex arrangements can be built by nesting panels.


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.