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.4 presents the JBoxTest application, which replaces the JPanel instance configured with a BoxLayout layout manager from the previous section with a JBox instance. The static createVerticalJBox method in the JBox class is used to create a box configured with a BoxLayout instance aligned along its vertical axis. Notice how a border is added to the JBox instance itself in the next line. Keystroke management, as shown in Chapter 3, “HTML Tools,” could also be added to the JBox instance. The resulting application looks and behaves identically to that shown in Figure 13.9.

Listing 13.4 The JBoxTest Application

package com.foley.test;

import java.awt.*;

import javax.swing.*;

import com.foley.utility.*;

/**
 * An application that displays a JBox panel.
 * Three labels are added to the panel. They are
 * separated with rigid areas and glue. This spaces
 * the labels in the preferred size of the panel,
 * and evenly spaces the labels in larger sizes.
 * <p>
 * @author Mike Foley
 **/
public class JBoxTest extends Object {

    /**
     * Application entry point.
     * Create the JBox and labels. Add the labels
     * to the jBox, and space them using glue
     * and rigid invisible components.
     * <p>
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        JFrame frame = new ApplicationFrame( “JBoxTest” );

        JPanel panel = JBox.createVerticalJBox();
        panel.setBorder( BorderFactory.createLoweredBevelBorder() );

        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() );

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

    } // main

} // JBoxTest

Summary

This chapter presented JPanel and Box, two relatively simple container classes contained in the JFC. The JPanel class provides a lightweight container that can be instantiated. It inherits the majority of its functionality from the JComponent class and contains very little additional code.

The Box class is an unusual JFC visual component in that it doesn’t inherit from the JComponent class. It’s a lightweight extension of the AWT Container class that comes configured with a BoxLayout layout manager. This layout manager is new to the JFC. Its options and usage were also presented in this chapter. (The BoxLayout class can be used with any container, not just the Box class.)

The Box class contains static methods to create invisible components that can add space in a container. Although these methods were presented in containers managed by a BoxLayout layout manager, they can be added to any container. The rigid component is of a fixed size that does not change. A glue component has a zero preferred size, but it can be expanded in one or both directions to fill the component, as required by the layout manager.

The JBox class was created to fill the void of the Box class. It is a descendant of the JComponent class, so it inherits the features contained in that class. This class is more consistent with other JFC visual components than the Box class.


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.