/Users/lyon/j4p/src/gui/run/RunTextField.java

1    package gui.run; 
2     
3    import javax.swing.*; 
4    import java.awt.event.*; 
5     
6    /** 
7     * Just like the RunTextFieldOld except that each 
8     * char causes an invocation of the run method 
9     */ 
10   public abstract class RunTextField extends 
11                                      JTextField 
12           implements ActionListener, 
13                      FocusListener, 
14                      Runnable { 
15       private String originalText = null; 
16    
17       /** 
18        * Invoked when a component gains the keyboard focus. 
19        */ 
20       public void focusGained(FocusEvent e){}; 
21    
22       /** 
23        * Invoked when a component loses the keyboard focus. 
24        */ 
25       public void focusLost(FocusEvent e){ 
26           run(); 
27       }; 
28    
29       public String getOriginalText() { 
30           return originalText; 
31       } 
32    
33    
34       public RunTextField(String text) { 
35           super(text); 
36           originalText = text; 
37           addActionListener(this); 
38           ShortcutUtils.addShortcut(this); 
39           addFocusListener(this); 
40    
41       } 
42    
43    
44       public RunTextField() { 
45           this(""); 
46       } 
47    
48       public RunTextField(int columns) { 
49           super(columns); 
50           addActionListener(this); 
51           addFocusListener(this); 
52       } 
53    
54       public RunTextField(String text, 
55                           int columns) { 
56           super(text, columns); 
57           addActionListener(this); 
58           addFocusListener(this); 
59       } 
60    
61       public RunTextField( 
62               javax.swing.text.Document doc, 
63               String text, 
64               int columns) { 
65           super(doc, text, columns); 
66           addActionListener(this); 
67           addFocusListener(this); 
68       } 
69    
70       public void actionPerformed( 
71               java.awt.event.ActionEvent e) { 
72           run(); 
73       } 
74    
75       public static void main(String args[]) { 
76           gui.ClosableJFrame cf = new gui.ClosableJFrame( 
77                   "RunTextField"); 
78           java.awt.Container c = cf.getContentPane(); 
79           c.add(new RunTextField( 
80                   "[aWhat is your name?") { 
81               public void run() { 
82                   System.out.println(getText()); 
83               } 
84           }); 
85           c.add(new RunTextField( 
86                   "[bWhat is your name?") { 
87               public void run() { 
88                   System.out.println(getText()); 
89               } 
90           }); 
91           c.setLayout( 
92                   new java.awt.GridLayout(4, 0)); 
93           cf.setSize(200, 200); 
94           cf.setVisible(true); 
95       } 
96    
97   }