SwingWorker - The Background Task Worker
This chapter provides tutorial notes and example codes on the java.swing.SwingWorker class. Topics include quick introduction of java.swing.SwingWorker; Example programs for catching final result, publishing intermediate values, updating progress property value, and connecting to JProgressBar components.
What Is SwingWorker Class?
SwingWorker Example using done() Method
SwingWorker Example using publish() Method
SwingWorker Example using "progress" Property
SwingWorker Example using JProgressBar
- The java.swing.SwingWorker class is a task worker, which performs time consuming tasks in background.
- A SwingWorker instance interacts with 3 threads: the Launching thread, the Worker thread and the Dispatch thread.
- The Launching thread calls the execute() method to kickoff the task to background and returns immediately.
- The Worker thread executes your own version of the doInBackground() method continuously in the background.
- The Dispatch thread wakes up from time to time to notify you on what happened in the Worker thread.
- When doInBackground() is ended, the Dispatch thread notifies you by call your version of the done() method.
- To publish intermediate values, you can call the publish(V) method in doInBackground().
The Dispatch thread notifies you by calling your version of the process(List<V>) method.
- To update the progress property, you can call the setProgress(i) method in doInBackground().
The Dispatch thread notifies you by calling your version of a PropertyChangeListener class.
- The java.swing.JProgressbar class is a UI component designed to show the progress of a background task
implemented as SwingWorker instance.
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
PDF Printing Version