Chapter 6. Bounded Range Components
6.10. Sliders in the three look-and-feels
6.3.8 Creating a Slider
The following program shows how to create a full-featured slider:
// SliderExample.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*;
public class SliderExample extends JPanel { public SliderExample() {
super(true);
this.setLayout(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25); slider.setMinorTickSpacing(2);
slider.setMajorTickSpacing(10); slider.setPaintTicks(true); slider.setPaintLabels(true);
// Note that the following line is really unnecessary. By setting a // positive integer to the major tick spacing and setting the paintLabel // property to true, it's done for us!
slider.setLabelTable(slider.createStandardLabels(10)); add(slider, BorderLayout.CENTER);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Slider Example");
frame.addWindowListener(new BasicWindowMonitor()); frame.setContentPane(new SliderExample()); frame.pack(); frame.setVisible(true); } }
Figure 6.12. Swing slider
6.4 The JProgressBar Class
Like sliders, progress bars are also a new feature in Swing. The bars themselves are simply rectangles of an arbitrary length, a percentage of which is filled in based on the value of their model. Applications typically use progress bars to report the status of time-consuming jobs, such as software installation or large amounts of copying. Swing progress bars come in two flavors:
horizontal and vertical. If the orientation is horizontal, the bar fills from left to right. If the bar is vertical, it fills from bottom to top. The class hierarchy is illustrated in Figure 6.13.
Figure 6.13. JProgressBar class diagram
Different look-and-feels can contain different filling styles. Metal, for example, uses a solid fill, while the Windows look-and-feel uses an LED style. The latter means that the bar indicates progress by filling itself with dark, adjacent rectangles instead of using a fluid line. The
JProgressBar class also contains a boolean that specifies whether the progress bar draws a dark
border around itself. You can override this default border by setting the border property of the
JComponent. Figure 6.14 shows a Swing progress bar with three different look-and-feels.
Figure 6.14. Progress bars in the three look-and-feels
6.4.1 Properties
The basic properties of the JProgressBar object are listed in Table 6.5. The orientation decides
which way the progress bar lies; it must be either JProgressBar.HORIZONTAL or
JProgressBar.VERTICAL. The minimum , maximum, and value properties mirror those in the
bounded-range model. The booleanborderPainted indicates whether the component's border
should appear around the progress bar. Borders are routinely combined with progress bars—they not only tell the user where its boundaries lie, but they also help to set off the progress bar from other components. An important note about the JProgressBar class: there are no methods to access
the extent variable of its bounded-range model. This property is irrelevant in the progress bar
component.
Table 6.5, JProgressBar Properties
Property Data Type get is set bound Default
UI progressBarUI from L&F
UIClassID* String "ProgressBarUI"
model BoundedRangeModel DefaultBoundedRangeModel()
accessibleContext*AccessibleContext JProgressBar.AccessibleJProgressBar()
borderPainted boolean true
maximum int 100
minimum int 0
orientation int JProgressBar.HORIZONTAL
percentComplete double
string String null
stringPainted boolean false
value int 0
See also properties from the JComponent class (Table 3.5)
Three properties (all new in Swing 1.1/JDK 1.2) control whether a string is painted onto the
progress bar. StringPainted is true if the string should appear. The string property is the actual
string to be painted. If it is null, the progress bar displays the value of percentComplete ,
converted to a percentage between and 100 (e.g., "35%"). Finally, percentComplete holds the
completion value as a number between 0 and 1.
6.4.2 Events
JProgressBar triggers a ChangeEvent whenever the user modifies any of its properties and a PropertyChangeEvent when its bound property changes.
public void addChangeListener(ChangeListener l)
public void removeChangeListener(ChangeListener l)
Add or remove a specific listener for ChangeEvent notifications from the component.
protected void fireStateChanged()
Fires a ChangeEvent to each of the registered listeners. It is designed to be invoked only by
objects that subclass JProgressBar.
protected ChangeListener createChangeListener()
Returns an internal class, ModelListener, which is used to propagate change events from
the bounded-range model. Subclasses of JProgressBar can override this method to return
their own ChangeListener if they wish to handle changes from the model differently.
6.4.3 Protected Fields
protected BoundedRangeModel model
protected transient ChangeEvent changeEvent
Represents the change event for the JProgressBar object. Because the ChangeEvent does
not contain information about the property that has changed, but instead only that a change has occurred, only one instance of this object is needed for each event that JProgressBar
fires.
protected ChangeListener changeListener
Includes support for the ChangeEvent event model, including managing the ChangeListener event listeners.
protected int orientation
Indicates the orientation of the progress bar.
protected boolean paintBorder
Indicates whether the progress bar's border should be painted.
6.4.4 Constructors
public JProgressBar()
Creates a horizontal progress bar with a lowered border. The DefaultBoundedRangeModel
is used as the data model for the progress bar.
public JProgressBar(BoundedRangeModel model)
public JProgressBar(int orient, int min, int max)
public JProgressBar(int min, int max)
public JProgressBar(int orient)
These constructors create progress bars with initial values specified by their arguments. In the first of these constructors, model supplies the initial values and serves as the data model
of the progress bar.
6.4.5 Miscellaneous
public void paintBorder()
Overrides the paintBorder() method in JComponent to paint the border for the progress
bar, if the borderPainted property is currently true.
public void updateUI()
Signals that a new look-and-feel has been set using the setUI() accessor. Invoking this
method forces the slider component to reset its UI delegate.