![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
The JProgressBar Class
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 models 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
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 models 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 its 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 bars 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 models range. The resulting progress bar is shown in Figure 10.18.
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. SummaryThis 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 models range.
|
![]() |
|