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


The JProgressBar Class


Note:  
There are no bound properties defined in the JProgressBar class. If the model changes for the progress bar and a listener was listening to the old model, it will be unaware of the change. The listener will no longer receive change events from the model that controls the progress bar that it is interested in.

To avoid this type of error, the JProgressBar class allows a ChangeListener to be added to it directly, rather than to its BoundedRangeModel. The progress bar listens for changes in the model and forwards all ChangeEvents from the model to its registered listeners.


The JProgressBar class provides a class that can show the progress of an operation. A progress bar is commonly seen at the bottom of a frame when a lengthy operation is under way. It is updated periodically to indicate the status of the operation.

The JProgressBar class tracks the minimum, maximum, and current values in a BoundedRangeModel instance. The class contains the familiar convenience methods for setting and getting the model’s minimum, maximum, and current values. The default values for these properties define a range from 0 to 100, and an initial value of 0. These properties are not bound to the JProgressBar class; interested parties must listen to the model to be notified when these properties change.

The orientation of the progress bar can be specified with the setOrientation method. Legal values for the orientation property are the constants HORIZONTAL and VERTICAL, defined in the SwingConstants interface. The JProgressBar class implements this interface, so the constant can be specified as either SwingConstants.HORIZONTAL or JProgressBar.HORIZONTAL. The getOrientation method returns the current value of this property. The orientation property is not bound in the JProgressBar class.

The ProgressBarTest application, shown in Listing 10.22, creates a JProgressBar instance and adds it to a frame. The application uses the default range for the progress bar and sets its value to 25. The resulting progress bar is shown in Figure 10.17.

Listing 10.22 The ProgressBarTest Application

package com.foley.test;

import java.awt.*;

import javax.swing.*;

import com.foley.utility.ApplicationFrame;

/**
 * An application that displays a JProgressBar instance
 * in its frame.
 *
 * @author Mike Foley
 **/
public class ProgressBarTest extends Object {

    /**
     * Application entry point.
     * Create a frame, the progress bar and display it.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        JFrame frame = new ApplicationFrame( “ProgressBar Test” );
        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue( 25 );

        frame.getContentPane().add( progressBar, BorderLayout.CENTER );
        frame.pack();

        frame.setVisible( true );

    } // main

} // ProgressBarTest


Figure 10.17  The ProgressBarTest application.

Unlike what you saw with the JSlider class earlier in this chapter, the JProgressBar class contains a property for displaying the current value in the progress bar. Passing true to the setStringPainted method will paint the current value in the progress bar as a percentage of the model’s range. By default, the string is not painted. The string painted in the progress bar can be specified by calling the setString method. This method can be repeatedly called as the progress is incremented, if desired. Each time it’s called, the new string will be painted in the progress bar. If a string has been specified for the progress bar using this method, it is painted in the progress bar rather than calculating the completion percentage. The displayed string can be queried with the getString method. After a string has been specified, passing null to the setString method will restore the default behavior.

The isStringPainted method can be called to query the current state of the stringPainted property. The default behavior of the getString method calls the getPercentComplete method. This method returns a double that represents the percentage of the value in range defined in the progress bar’s model. The value is between 0.0 and 1.0.

Adding the following line of code to the ProgressBarTest application will cause the string to be painted as a percentage of the model’s range. The resulting progress bar is shown in Figure 10.18.


Figure 10.18  A string displayed in the JProgressBar.

progressBar.setStringPainted( true );

Passing false to the setBorderPainted method will hide the border around the progress bar. The isBorderPainted method will query the current state of the borderPainted property. The default behavior is to paint the border around the progress bar.

Summary

This chapter covered a lot of material. The ListModel and ListSelectionModel were presented, as well as how the JList class uses these two models. You also learned how to use the JList class to present the user with a choice of options.

The JComboBox class and its associated models, ComboBoxModel and MutableComboBoxModel, were presented. This class was compared and contrasted with the JList class and its models, and the inconsistencies between these classes were explained. Unfortunately, these inconsistencies make programming the two classes overly complex.

The BoundedRangeModel was presented, as well as three classes that use this model. You learned about the JSlider class. An extension to the class was presented to demonstrate how typed labels can be used in a slider. The JScrollBar class was introduced, which will be used more extensively in Chapter 15 when scroll panes are presented. Finally, you learned about the JProgressBar class, which provides a component that displays the value in its BoundedRange model as a percentage of the model’s range.


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.