What is the importance of a SwingWorker class in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A SwingWorker class enables us to perform an asynchronous task in a worker thread (such as a long-running task) then update Swing components from the Event Dispatch Thread (EDT) based on the task results. It was introduced in Java 1.6 Version.

SwingWorker class

  • The java.swing.SwingWorker class is a task worker, which performs time-consuming tasks in the background.
  • A SwingWorker instance interacts with 3 threads, Current thread, the Worker thread, and the Event Dispatch thread(EDT).
  • The Current thread calls the execute() method to kick off the task to the background and returns immediately.
  • The Worker thread executes our own version of the doInBackground() method continuously in the background.
  • The Event Dispatch Thread (EDT) wakes up from time to time to notify us of what happened in the Worker thread.
  • When doInBackground() is ended, the Event Dispatch Thread (EDT) notify us by call our version of the done() method.
  • To publish intermediate values, we can call the publish(V) method in doInBackground(). The Event Dispatch Thread (EDT) notifies us by calling our version of the process(List) method.
  • To update the progress property, we can call the setProgress(i) method in doInBackground(). The Event Dispatch Thread (EDT) notifies us by calling our 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 a SwingWorker instance.

Example

import java.awt.*;
import java.awt.event.*;;
import javax.swing.*;
public class SwingWorkerTest {
   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         @Override
         public void run() {
            JFrame frame = new JFrame();
            JButton button = new JButton();
            button.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                  new GUIWorker().execute();
               }
            });
            button.setText("Click Me !!!");
            frame.getContentPane().add(button);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setSize(350, 300);
            frame.setVisible(true);
         }
      });
   }
}
class GUIWorker extends SwingWorker {
   private JFrame frame = new JFrame();
   private JDialog dialog = new JDialog(frame, "Swingworker Test", true);
   private JProgressBar progressBar = new JProgressBar();
   public GUIWorker() {
      progressBar.setString("Waiting on time");
      progressBar.setStringPainted(true);
      progressBar.setIndeterminate(true);
      dialog.getContentPane().add(progressBar);
      dialog.setSize(350, 300);
      dialog.setModal(false);
      dialog.setLocationRelativeTo(null);
      dialog.setVisible(true);
   }
   @Override
   protected Integer doInBackground() throws Exception {
      System.out.println( "GUIWorker doInBackground()" );
      Thread.sleep(10000);
      return 0;
   }
   @Override
   protected void done() {
      System.out.println("done");
      JLabel label = new JLabel("Task Complete");
      dialog.getContentPane().remove(progressBar);
      dialog.getContentPane().add(label);
      dialog.getContentPane().validate();
   }
}

Output

raja
Published on 08-Jul-2019 13:04:03
Advertisements