javaInterface: ThreadedActionListener.java

File ThreadedActionListener.java, 1.0 kB (added by goskab, 6 months ago)

Threaded action listener to help with swing problems

Line 
1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3 import java.util.ArrayList;
4
5 /**
6  * This is an action listener that will start a new thread instead
7  * of blocking the EventQueue ("freezing" the GUI).
8  *
9  * Use this wherever you have to poll the oswaldInterface
10  *
11  * @author goskab
12  *
13  */
14 public abstract class ThreadedActionListener extends Thread implements ActionListener, Cloneable {
15         ThreadedActionListener thread = null;
16         /**
17          * Don't touch this method, it starts up the proper thread
18          */
19         public void actionPerformed(ActionEvent arg0) {
20                 try {
21                         //check if our thread is dead, we only want one running at once
22                         if ((thread != null && !thread.isAlive()) || thread == null) {
23                                 //clone the current object, and start that :)
24                                 thread = (ThreadedActionListener)this.clone();
25                                 thread.start();
26                         }
27                 }
28                 catch (Exception e) {
29                         System.out.println("Exception thrown while cloning. This shouldn't happen");
30                 }
31         }
32        
33         /**
34          * Your normal "actionPerformed" code goes in this method
35          */
36         public abstract void run();
37
38 }