javaInterface: ThreadTester.java

File ThreadTester.java, 1.4 kB (added by goskab, 6 months ago)

Test class to show how to use ThreadedActionListener?

Line 
1 import javax.swing.JButton;
2 import javax.swing.JFrame;
3
4 /**
5  * Test class for the new ThreadedActionListener
6  *
7  * This is an example for you to use as a reference
8  *
9  * @author goskab
10  *
11  */
12 public class ThreadTester extends JFrame {
13         private OswaldInterface oi;
14         private JButton toggle;
15
16         /**
17          * Starter method
18          * @param args doesn't do anything
19          */
20         public static void main(String[] args) {
21                 //test out the new action listener
22                 ThreadTester tr = new ThreadTester();
23                 tr.setVisible(true);
24         }
25        
26         /**
27          * Start up the extra interface
28          *
29          */
30         public ThreadTester() {
31                 oi = new OswaldInterface();
32                 this.setLayout(null);
33                 this.setSize(130, 100);
34                
35                 toggle = new JButton("Run");
36                 this.add(toggle);
37                 toggle.setSize(100,50);
38                 toggle.setLocation(10,10);
39                 toggle.setVisible(true);
40                 //Add the new type of action listener
41                 toggle.addActionListener(new MyActionListener());
42                
43                 this.setVisible(true);
44         }
45        
46         /**
47          * My new super awesome actionListener class
48          *
49          * NOTE: extends ThreadedActionListener not implements ActionListener
50          *
51          * This will work on both the oswald and a computer (without blocking guis)
52          *
53          * @author goskab
54          *
55          */
56         public class MyActionListener extends ThreadedActionListener {
57
58                 @Override
59                 public void run() {
60                         System.out.println("Ran my action Listener");
61                         //wait for a hit on the oswald
62                         while (oi.getAccelZ() < 10000) {
63                                
64                         }
65                         System.out.println("Oswald hit!");
66                 }
67                
68         }
69 }