HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Swing, Threads, and Events"
Vol. 8, Issue 12, p. 39

	



Listing 1

public void actionPerformed(
    ActionEvent evt) {
  //Calling a method that takes a long
  //time to return
  waiter();
}

private void waiter() {
  //I am a method that takes a long
  //time to return
  try {
    Thread.sleep(60000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

Listing 2

public ExampleClass extends JFrame
    implements Runnable {
  private JTextField example1;
  private JTextField example2;

  public ExampleClass() {
    getContentPane().setLayout(
      new FlowLayout());
    example1 = new JTextField(
      "Field 1");
    example2 = new JTextField(
      "Field 2");
    getContentPane().add(example1);
    getContentPane().add(example2);
    Thread t = new Thread(this);
    t.start();
  }
  
  public void run() {
    //I am not the event thread,
    //I should not be updating the GUI
    example1.setText("Updated");
  }
}

Listing 3

public void actionPerformed(
    ActionEvent evt) {
  Thread t = new Thread() {
    public void run() {
      waiter();
    }
  };
  t.start();
}

private void waiter() {
  //I am a method that takes a long
  //time to return
  try {
    Thread.sleep(60000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

Listing 4

class WorkerThread extends Thread {
  public void run() {
    waiter();
  }
}
public void actionPerformed(
    
		ActionEvent evt) {
  WorkerThread wt = new WorkerThread();
  wt.start();
}

private void waiter() {
  //I am a method that takes a long
  //time to return
  try {
    Thread.sleep(60000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}

Listing 5

class WorkerThread extends Thread {
  public void run() {
    //I am a method that takes a long
    //time to return
    try {
      Thread.sleep(60000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

public void actionPerformed(
    ActionEvent evt) {
  WorkerThread wt = new WorkerThread();
  wt.start();
}

Listing 6

Runnable example = new Runnable() {
  public void run() {
    System.out.println(
      "Example running from " +
      Thread.currentThread());
  }
};
SwingUtilities.invokeLater(example);
System.out.println("This probably will "
  +"be executed before the example "
  +"thread");

Listing 7

Runnable example = new Runnable() {
  public void run() {
    System.out.println(
      "Example running from " +
      Thread.currentThread());
  }
};
try {
  SwingUtilities.
    invokeAndWait(doHelloWorld);
} catch (InterruptedException e) {
  e.printStackTrace();
} catch (InvocationTargetException e) {
  e.printStackTrace();
  //Wrapper for an exception that was
  //thrown from our example
}

Listing 8

public class MyWorker 
    extends SwingWorker {
  JFrame parent;
  
  public MyWorker(JFrame p) {
    parent = p;
  }
  
  public Object construct() {
    for (int i = 0; i < 10; i++) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
  public void finished() {
    JOptionPane.showMessageDialog(
      parent, "I'm finished!");
  }
}

public class MyFrame extends JFrame 
  implements ActionListener {
  private JButton _pushMe;
  private JButton _stopMe;
  private MyWorker myWorker
  
  public MyFrame() {
    JPanel buttons = new JPanel(
      new FlowLayout());
    _pushMe = new JButton("Push Me");
    _stopMe = new JButton("Stop Me");
    _pushMe.addActionListener(this);
    _stopMe.addActionListener(this);
    buttons.add(_pushMe);
    buttons.add(_stopMe);
    getContentPane().add(buttons,
      BorderLayout.NORTH);
  }
  
  public void actionPerformed(
      ActionEvent evt) {
    if (evt.getSource() == _pushMe) {
      myWorker = new MyWorker();
      myWorker.start();
    } else {
      if (myWorker != null) {
        myWorker.interrupt();
      }
    }
  }
}

Listing 9

public class ThreadedFrame extends 
    JFrame {
  private static int initCounter = 0;
  
  private JTabbedPane _tabbedPane;
  
  public ThreadedFrame() {
    getContentPane().setLayout(
      new BorderLayout());
    _tabbedPane = new JTabbedPane();
    getContentPane().add(_tabbedPane,
      BorderLayout.CENTER);

    _tabbedPane.addTab("Tab One", 
      new TabOne());
    _tabbedPane.addTab("Tab Two",
      new TabTwo());
    _tabbedPane.addTabe("Tab Three", 
      new TabThree());
    _tabbedPane.addTab("Tab Four",
      new TabFour());
      
    while (initCounter > 0) {
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    
    pack();
    setVisible(true);
  }

  public synchronized static final void 
      incCounter() {
    initCounter++;
  }

  public synchronized static final void 
      decCounter() {
    initCounter--;
  }
}

public class TabOne extends JPanel 
  implements Runnable {

  public TabOne() {
    ThreadedFrame.incCounter();
    Thread t = new Thread(this);
    t.start();
  }

  public void run() {
    //Initialize the GUI code here
    ThreadedFrame.decCounter();
  }
}

public class TabTwo extends JPanel 
  implements Runnable {
    
  public TabTwo() {
    ThreadedFrame.incCounter();
   		Thread t = new Thread(this);
    t.start();
  }
  
  public void run() {
 //Initialize the GUI code here
    ThreadedFrame.decCounter();
  }
}

public class TabThree extends JPanel 
  implements Runnable {
    
  public TabThree() {
    ThreadedFrame.incCounter();
    Thread t = new Thread(this);
    t.start();
  }
  
  public void run() {
    //Initialize the GUI code here
    ThreadedFrame.decCounter();
  }
}

public class TabFour extends JPanel 
  implements Runnable {

    public TabFour() {
    ThreadedFrame.incCounter();
    Thread t = new Thread(this);
    t.start();
  }
  
  public void run() {
    //Initialize the GUI code here
    ThreadedFrame.decCounter();
  }
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.