Use SwingWorker to perform background tasks : SwingWorker « Swing JFC « Java






Use SwingWorker to perform background tasks

    
 
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

public class Main extends JFrame {

  public Main() {
    this.setLayout(new FlowLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton processButton = new JButton("Start");
    JButton helloButton = new JButton("Hello");

    processButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        MyTask process = new MyTask();
        try {
          process.execute();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

    helloButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Hello There");
      }
    });
    this.getContentPane().add(processButton);
    this.getContentPane().add(helloButton);

    this.pack();
    setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
  }

}

class MyTask extends SwingWorker {
  protected Object doInBackground() throws Exception {
    Integer result = new Integer(0);
    for (int i = 0; i < 10; i++) {
      result += i * 10;
      try {
        Thread.sleep(5000);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return result;
  }
}

   
    
    
    
  








Related examples in the same category

1.Use SwingWorker to wrap time consuming task
2.SwingWorker in Java 6
3.Generic class to dispatch events in a highly reliable way.
4.Detect Event Dispatch Thread rule violations
5.Process On Swing Event Thread
6.A pool of work threads
7.3rd version of SwingWorker
8.This program demonstrates a swing-worker thread that runs a potentially time-consuming task