//package multithreading;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class GUIApp {
   private String letters ="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
   private int index = 0;

   /**
    * @param args - not used
    */
   public static void main(String[] args) {
      System.out.println("main() started on thread " + Thread.currentThread().getId() );
      GUIApp me = new GUIApp(); // create an instance of ourself

      me.initFrame(); // create the UI

      me.methodA();	// // invoke a method that takes a while to execute...

      System.out.println("Done!!!");

   }

   // create a UI containing a single pushbutton
   public void initFrame() {
      JFrame jf = new JFrame();
      jf.setTitle("Multi-threading app");
      JButton jb = new JButton("Push me!");
      jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // HIDE_ON_CLOSE is default
      jf.getContentPane().add(jb);

      ActionListener bh = new ActionListener() { // anonymous inner class 
            public void  actionPerformed(ActionEvent e) {
               System.out.println("Button clicked on thread " + Thread.currentThread().getId() );
            }
         };
      jb.addActionListener(bh);

      jf.pack();
      jf.setVisible(true);
   }

   // This method prints the contents of a String to the console,
   // pausing a little between each character, so that it takes a while to complete.
   private void methodA() {
      long id = Thread.currentThread().getId();
      while( index < letters.length() ) {
         System.out.println(letters.charAt(index) +","+id );
         index++;
         try {
            Thread.sleep(250);
         } catch (InterruptedException e) { // we were awakened early...
            // do nothing; just continue while() loop
         }
      }
   }
}
