Java Reference
In-Depth Information
JProgressBar Class
Swing's
JProgressBar
is different from the other
BoundedRangeModel
components. Its main
purpose is not to get input from the user, but rather to present output. This output is in the
form of a process completion percentage. As the percentage increases, a bar progresses across
the component until the job is completed and the bar is filled. The movement of the bar is
usually part of some multithreaded task, to avoid affecting the rest of the application.
Figure 12-12 shows several sample
JProgressBar
components. The top bar uses all the
display defaults. The bottom bar adds a border around the component and displays the
completion percentage. The right bar removes the border, and the left bar has a fixed string
present instead of a completion percentage.
Figure 12-12.
Sample JProgressBar components
From an object-oriented perspective, there are two primary parts to a
JProgressBar
:
The familiar
BoundedRangeModel
stores the data model for the component, and the
ProgressUI
is the user interface delegate.
■
Note
To display a progress bar in a dialog box, use the
ProgressMonitor
class discussed in Chapter 9.
Creating JProgressBar Components
There are five different constructors for
JProgressBar
:
public JProgressBar()
JProgressBar aJProgressBar = new JProgressBar();
public JProgressBar(int orientation)
// Vertical
JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
// Horizontal
JProgressBar bJProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);
public JProgressBar(int minimum, int maximum)
JProgressBar aJProgressBar = new JProgressBar(0, 500);