// @topic T11620 Swing demo A1 with animation
// @brief JFrame, JButton, and Thread.sleep() example
/*
 * @author ik
 *
 */

package pckg;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SwingMain {
    private static JButton button = null;
    private static JFrame frame = null;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });

        // animation:
        for ( int count = 1; count < 500; ++ count ) {
            // this is main thread running
            try {
                Thread.sleep( 1000/*milliseconds*/ );
            } catch ( InterruptedException ex ) {
            }
            
            // ***WRONG*** cannot touch GUI from main thread:
            // button.setText( "My name is BUTTON " + count );
            // frame.setTitle( "Frame " + count );

            // CORRECT: delegate any ineraction with
            // swing GUI to the event dispatch thread:
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // this will execute on EDT:
                    button.setText( "My name is BUTTON " + count );
                    frame.setTitle( "Frame " + count );
                }
            });
            
        }
    }//main  
    
    public static void createAndShowGUI() {
        // Swing test
        frame = new JFrame("JFrame is me");
        frame.setTitle( "Frame Title" );
        frame.setBounds(0, 0, 400, 300);

        button = new JButton("Hello");
        frame.getContentPane().add(button); // add button to layout

        frame.setVisible(true);
    } //createAndShowGUI
    
}//class SwingMain