Thursday, September 27, 2012

SwingWorker

javax.swing.SwingWorker is an abstract class to perform lengthy GUI-interaction tasks in a background thread. It's a working example to use javax.swing.SwingWorker.

SwingWorker Example


package javatesttimer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestTimer {
    
    static int counter = 0;
    static JFrameWin jFrameWindow;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(runJFrameLater);
    }
   
    static Runnable runJFrameLater = new Runnable() {
        
        @Override
        public void run() {
            jFrameWindow = new JFrameWin();
            jFrameWindow.setVisible(true);
        }
     
    };
    
    public static class JFrameWin extends JFrame{
        
        JLabel label;
        
        public JFrameWin(){
            this.setTitle("java-buddy.blogspot.com");
            this.setSize(300, 200);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            label = new JLabel();
            label.setText("un-initialized!");
            
            JButton buttonStart = new JButton("Start SwingWorker");
            buttonStart.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    new MySwingWorker(label).execute();
                }
            });
            
            Box verticalBox = Box.createVerticalBox();
            
            verticalBox.add(buttonStart);
            verticalBox.add(label);
            this.add(verticalBox);
        }
        
        public void setLabel(String l){
            label.setText(l);
        }
    }
    
    public static class MySwingWorker extends SwingWorker<String, Integer>{
        
        private JLabel workerLabel;

        //Constructor
        public MySwingWorker(JLabel workerLabel) {
            this.workerLabel = workerLabel;
        }

        //in background thread
        @Override
        protected String doInBackground() throws Exception {
            
            for(int i = 0; i < 10; i++){
                publish(i);
                Thread.sleep(1000); //Perform long-time job
            }
            return "DONE";
        }

        //in EDT
        @Override
        protected void process(List<Integer> chunks) {
            for(Integer chunk : chunks){
                workerLabel.setText(String.valueOf(chunk));
            }
        }

        //in EDT
        @Override
        protected void done() {
            try {
                workerLabel.setText(get());
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaTestTimer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(JavaTestTimer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
}


Related:
- javax.swing.Timer and java.util.Timer

No comments:

Post a Comment