Using the JFC/Swing Packages |
[PENDING: currently this page covers only the use of progress bars, but we have plans to expand it to also coverProgressMonitor
andProgressMonitorInputStream
. ]Technically speaking, a
JProgressBar
displays an integer value within a bounded interval. Realistically speaking, you use aJProgressBar
to display the progress of a long-running task.JProgressBar
is for display purposes only. To allow the user to set a bounded integer value, use a slider.Here's a picture of a small demo application that uses a progress bar to measure the progress of a long task running in a separate thread:
Below is the code from
Try this:
- Compile and run the application. The main source file is
ProgressBarDemo.java
. You will also needLongTask.java
andSwingWorker.java
.
See Getting Started with Swing if you need help.- Push the Start button. Watch the progress bar as the task makes progress.
ProgressBarDemo.java
that creates and sets up the progress bar:Once the task has begun, a timer causes the progress bar to update every second until the task completes. The timer is implemented with theprogressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(task.getLengthOfTask()); progressBar.setValue(0); . . . //add the progress bar to the window's content pane contentPane.add(progressBar);Timer
class, which is described in How to Use Timers. The progress bar measures the progress made by the task each second, not the elapsed time. The following line of code appears in theactionPerformed
method of the timer's action listener.Once a second, the timer fires an action event, which causes the program to find the amount of work completed by the task and use that value to update the progress bar.progressBar.setValue(task.getCurrent());As mentioned, the long-running task in this program runs in a separate thread. The long-running task is implemented by
LongTask.java
which uses aSwingWorker
. See Using the SwingWorker Class in Threads and Swing for information about theSwingWorker
class.
The Progress Bar API
The following tables list the commonly usedJProgressBar
constructors and methods. Other methods you're likely to call are defined by theJComponent
andComponent
classes and include [PENDING: anything in particular for JProgressBar?]. [Link to JComponent and Component discussions.]The API for using progress bars falls into two categories:
Setting or Getting the Progress Bar's Constraints/Values Method Purpose void setValue(int)
int getValue()Set or get the current value of the progress bar. The value is constrained by the minimum and maximum values. void setMinimum(int)
int getMinimum()Set or get the minimum value of the progress bar. void setMaximum(int)
int getMaximum()Set or get the maximum value of the progress bar. void setModel(BoundedRangeModel)
BoundedRangeModel getMaximum()Set or get the model used by the progress bar. The model establishes the progress bar's constraints and values. So you can use this method as an alternative to using the individual set/get methods listed above.
Fine Tuning the Progress Bar's Appearance Method Purpose void setOrientation(int)
int getOrientation()Set or get whether the progress bar is vertical or horizontal. Acceptable values are JProgressBar.VERTICAL
orJProgressBar.HORIZONTAL
.void setBorderPainted(boolean)
boolean isBorderPainted()Set or get whether the progress bar has a border. Examples that Use JProgressBar
This table shows the examples that useJProgressBar
and where those examples are described.
Example Where Described ProgressBarDemo.java
This page and How to Use Timers
Using the JFC/Swing Packages |