The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
SwingWorker
supports
bound properties, which are useful for communicating with other threads. Two bound properties are predefined: progress
and state
. As with all bound properties, progress
and state
can be used to trigger event-handling tasks on the event dispatch thread.
By implementing a property change listener, a program can track changes to progress
, state
, and other bound properties. For more information, refer to
How to Write a Property Change Listener in
Writing Event Listeners.
progress
Bound VariableThe progress
bound variable is an int
value that can range from 0 to 100. It has a predefined setter method (the protected
SwingWorker.setProgress
) and a predefined getter method (the public
SwingWorker.getProgress
).
The
example uses ProgressBarDemo
progress
to update a ProgressBar
control from a background task. For a detailed discussion of this example, refer to
How to Use Progress Bars in
Using Swing Components.
state
Bound VariableThe state
bound variable indicates where the SwingWorker
object is in its life cycle. The bound variable contains an enumeration value of type SwingWorker.StateValue
. Possible values are:
PENDING
doInBackground
is invoked.STARTED
doInBackground
is invoked until shortly before done
is invoked.The current value of the state
bound variable is returned by
SwingWorker.getState
.
Two methods, part of the Future
interface, also report on the status of the background task. As we saw in
Canceling Background Tasks, isCancelled
returns true
if the task has been canceled. In addition, isDone
returns true
if the task has finished, either normally, or by being cancelled.