A component that visually displays the progress of some task. As the task
progresses towards completion, the progress bar displays the
task's percentage of completion.
This percentage is typically represented visually by a rectangle which
starts out empty and gradually becomes filled in as the task progresses.
In addition, the progress bar can display a textual representation of this
percentage.
JProgressBar
uses a BoundedRangeModel
as its data model,
with the value
property representing the "current" state of the task,
and the minimum
and maximum
properties representing the
beginning and end points, respectively.
To indicate that a task of unknown length is executing,
you can put a progress bar into indeterminate mode.
While the bar is in indeterminate mode,
it animates constantly to show that work is occurring.
As soon as you can determine the task's length and amount of progress,
you should update the progress bar's value
and switch it back to determinate mode.
Here is an example of creating a progress bar,
where task
is an object (representing some piece of work)
which returns information about the progress of the task:
progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);
Here is an example of querying the current state of the task, and using
the returned value to update the progress bar:
progressBar.setValue(task.getCurrent());
Here is an example of putting a progress bar into
indeterminate mode,
and then switching back to determinate mode
once the length of the task is known:
progressBar = new JProgressBar();
...//when the task of (initially) unknown length begins:
progressBar.setIndeterminate(true);
...//do some work; get length of task...
progressBar.setMaximum(newLength);
progressBar.setValue(newValue);
progressBar.setIndeterminate(false);
For complete examples and further documentation see
How to Monitor Progress,
a section in The Java Tutorial.
Warning: Swing is not thread safe. For more
information see Swing's Threading
Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans
package.
Please see XMLEncoder
.