Java Swing Tutorials - Herong's Tutorial Examples
∟SwingWorker - The Background Task Worker
∟What Is SwingWorker Class?
This section describes what is javax.swing.SwingWorker class - An abstract class introduced in JDK 1.6 as part of the Swing package, that you can extend the SwingWorker class to perform a time intensive computing task in the background to avoid holding the UI component response time for too long.
What Is SwingWorker Class?
javax.swing.SwingWorker class is an abstract class introduced in JDK 1.6 as part of the Swing package.
You can extend the SwingWorker class to perform a time intensive computing task in the background to avoid holding the UI component
response time for too long.
When using the SwingWorker class to perform a background task,
there are three threads involved in the execution flow:
- Launching thread: The thread that creates a SwingWorker instance and calls the execute() on the instance.
- Worker thread: The thread that actually performs the bask by executing the doInBackground() method.
- Dispatch thread: The thread that is used by the Worker thread to send out information.
To extending the SwingWorker class, you need to consider the following:
- Implementing the doInBackground() method to perform the actual task.
- Implementing the done() method, if you want to do something when the task is done.
- Calling the get() method on the SwingWorker instance, if you want to retrieve the result
returned by the doInBackground() method.
Note that you should call the get() method after the task is done.
If you call it too early, it will wait for the task to finish.
- Calling the publish() method while performing calculation inside the doInBackground() method
any time when you want to publish intermediate values.
Of course, you need to implement the process() method to receive those intermediate values.
- Dividing the task in multiple equal units in the doInBackground() method and
calling the setProgress() method to update the "progress" property with a percentage value after each unit is completed.
Of course, you need to implement a java.beans.PropertyChangeListener class to get notified
about the "progress" value changes.
- Implementing a java.beans.PropertyChangeListener class to get notified
about the "status" value changes of the SwingWorker instance, going from PENDING, STARTED, to DONE.
Last update: 2014.
Table of Contents
About This Book
Introduction of Java Swing Package
Graphics Environment of the Local System
JFrame - Main Frame Class
JLabel - Swing Label Class
JButton - Swing Button Class
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
LookAndFeel and UIManager
Option Dialog Boxes
JEditorPane - The Editor Pane Class
►SwingWorker - The Background Task Worker
►What Is SwingWorker Class?
SwingWorker Example using done() Method
SwingWorker Example using publish() Method
SwingWorker Example using "progress" Property
SwingWorker Example using JProgressBar
References
PDF Printing Version