SwingWorker
supports bound properties, which are useful for communicating with other threads. Two bound properties are predefined:progress
andstate
. As with all bound properties,progress
andstate
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.The
Theprogress
Bound Variableprogress
bound variable is anint
value that can range from 0 to 100. It has a predefined setter method (the protectedSwingWorker.setProgress
) and a predefined getter method (the publicSwingWorker.getProgress
).The
example uses
ProgressBarDemo
progress
to update aProgressBar
control from a background task. For a detailed discussion of this example, refer to How to Use Progress Bars in Using Swing Components.The
Thestate
Bound Variablestate
bound variable indicates where theSwingWorker
object is in its lifecycle. The bound variable contains an enumeration value of typeSwingWorker.StateValue
. Possible values are:The current value of the
PENDING
- The state during the period from the construction of the object until just before
doInBackground
is invoked.STARTED
- The state during the period from shortly before
doInBackground
is invoked until shortly beforedone
is invoked.- DONE
- The state for the remainder of the existence of the object.
state
bound variable is returned bySwingWorker.getState
.Status Methods
Two methods, part of theFuture
interface, also report on the status of the background task. As we saw in Canceling Background Tasks,isCancelled
returnstrue
if the task has been canceled. In addition,isDone
returnstrue
if the task has finished, either normally, or by being cancelled.