Wednesday, September 26, 2012

SwingUtilities.isEventDispatchThread(), determine if the current thread is an AWT event dispatching thread.

Last post describe about javax.swing.Timer and java.util.Timer, Java API provide a SwingUtilities.isEventDispatchThread() method to determine if the current thread is an AWT event dispatching thread.

The last exercise is modified to using the method SwingUtilities.isEventDispatchThread() to determine if it's need to create another Runnable or not.

SwingUtilities.isEventDispatchThread(), determine if the current thread is an AWT event dispatching thread.


package javatesttimer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 *
 * @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);
    }
    
    //if it's in Event Dispatch Thread, setLabel() directly.
    //Otherwise, create a Runnable and invoke later to call setLabel() in EDT.
    private static void setLabelAtEDT(final int c){
        
        if(SwingUtilities.isEventDispatchThread()){
            jFrameWindow.setLabel("Directly: " 
                                + String.valueOf(c));
        }else{
            SwingUtilities.invokeLater(
                                new Runnable(){
                                    
                                    @Override
                                    public void run() {
                                        jFrameWindow.setLabel("invokeLater: " 
                                                + String.valueOf(c));
                                        counter++;
                                    }
                                }
                        );
        }
        
    }
    
    //Create Timer of javax.swing.Timer
    private static void testSwingTimer(){
        javax.swing.Timer swingTimer = new javax.swing.Timer(
                5000,
                new ActionListener(){
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setLabelAtEDT(counter);
                        counter++;
                    }
        });
        
        swingTimer.start();
    }
    
    //Create Timer of java.util.Timer
    private static void testUtilTimer(){
        java.util.Timer utilTimer = new java.util.Timer();
        
        utilTimer.schedule(
                new TimerTask(){
                    
                    @Override
                    public void run() {
                        setLabelAtEDT(counter);
                    }
                
                }, 
                0, 
                5000);
    }
    
    static Runnable runJFrameLater = new Runnable() {
        
        @Override
        public void run() {
            jFrameWindow = new JFrameWin();
            jFrameWindow.setVisible(true);
            
            //testSwingTimer();
            testUtilTimer();
        }
     
    };
    
    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!");
            
            this.add(label);
        }
        
        public void setLabel(String l){
            label.setText(l);
        }
    }
}


No comments:

Post a Comment