/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/logPolar/ToggleCommandButton.java

1    package bookExamples.ch26Graphics.carl.logPolar; 
2     
3    import bookExamples.ch26Graphics.carl.logPolar.CommandButton; 
4     
5    //package runJgui; 
6     
7     
8    public abstract class ToggleCommandButton 
9            extends CommandButton { 
10       // inherits a need for "gui.run()" from graphics.carl.CommandButton 
11       // to become concrete class 
12       private boolean toggled = false; 
13       private String s[] = new String[2]; 
14    
15       public ToggleCommandButton(String s0, String s1) { 
16           super(s0); 
17           s[0] = s0; 
18           s[1] = s1; 
19       } 
20    
21       public void actionPerformed(java.awt.event.ActionEvent e) { 
22           toggled = !toggled; 
23           if (toggled) 
24               setText(s[1]); 
25           else 
26               setText(s[0]); 
27           System.out.println("label=" + getText()); 
28           super.actionPerformed(e); 
29       } 
30    
31       public boolean isSelected() { 
32           return toggled; 
33       } 
34    
35       // main for testing and demo purposes only 
36       public static void main(String args[]) { 
37           System.out.println("Testing graphics.carl.ToggleCommandButton via main"); 
38           java.awt.Frame f = new java.awt.Frame(); 
39           f.setLayout(new java.awt.GridLayout(1, 0)); 
40           ToggleCommandButton 
41                   tcb = new 
42                           ToggleCommandButton("Toggle off", "Toggle On") { 
43                               public void run() { 
44                                   System.out.println("gui.run in tcb"); 
45                                   if (this.isSelected()) 
46                                       setBackground(java.awt.Color.blue); 
47                                   else 
48                                       setBackground(java.awt.Color.green); 
49                               } 
50                           }; 
51           f.add(tcb); 
52           f.setSize(200, 200); 
53           f.setBackground(java.awt.Color.red); 
54           f.show(); 
55       } 
56   } 
57