KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Actions


1 import org.swixml.SwingEngine;
2
3 import javax.swing.*;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6 import java.awt.event.WindowAdapter JavaDoc;
7 import java.awt.event.WindowEvent JavaDoc;
8
9 /**
10  * The Actions class shows how to use the
11  * <code>Actions</code> and <code>ActionCommand</code> attributes
12  *
13  * @author <a HREF="mailto:wolf@paulus.com">Wolf Paulus</a>
14  * @version $Revision: 1.1 $
15  * @since swixml #065
16  */

17 public class Actions extends WindowAdapter JavaDoc implements ActionListener JavaDoc {
18   private SwingEngine swix;
19
20   public JMenuItem mi_exit, mi_save;
21   public JPanel pnl_North;
22   //
23
// For every Actions, there needs to be a
24
// public AbstractAction member variables with an anonymous inner class instantiation
25
//
26

27   /** <code>Action</code> newAction handles the <em>new</em> action attribute */
28   public Action newAction = new AbstractAction() {
29
30     public void actionPerformed(ActionEvent JavaDoc e) {
31       System.out.println( "New" ); // show the access outer class member ..
32
this.setEnabled( false ); // disables ALL buttons that are tied to this action
33
}
34   };
35
36   /** <code>Action</code> modifyAction handles the <em>modify</em> action attribute */
37   public Action openAction = new AbstractAction() {
38     /** Invoked when an action occurs. */
39     public void actionPerformed(ActionEvent JavaDoc e) {
40       System.out.println( "Open" );
41     }
42   };
43
44   /** <code>Action</code> petAction handles the <em>combobox</em> */
45   public Action petAction = new AbstractAction() {
46      public void actionPerformed(ActionEvent JavaDoc e) {
47        System.out.println( ((JComboBox) e.getSource()).getSelectedItem().toString() );
48      }
49    };
50
51
52   /**
53    * Constructs a new Actions object, registering action handlers for center_panel components.
54    */

55   private Actions() {
56     try {
57       swix = new SwingEngine( this );
58       swix.render( "xml/actions.xml" );
59
60       // at this point all AbstractActions are linked with the button etc.
61
// ActionCommands however need to be linked manually, see below ...
62

63       // add this class as an action listener to all buttons inside the panel with the id = center_panel
64
swix.setActionListener( pnl_North, this );
65       // add this class as an action listener to MenuItem with the id = mi_exit.
66
mi_exit.addActionListener( this );
67       // add this class as an action listener to MenuItem with the id = mi_save
68
mi_save.addActionListener( this );
69       //
70
// Note, the mi_about MenuItem was not linked at all so far. Therefore, no action is performed when this
71
// menu item gets requested.
72
// The Toolbar button with the Actions="newAction" attribute is covered twice,
73
// during parsing the AbstactAction newAction is linked in and later, the setActionListener() adds
74
// this object's actionPerformed(). Therefore, when clicked, both actionPerformed() methods are getting called
75
//
76
swix.getRootComponent().setVisible(true);
77     } catch (Exception JavaDoc e) {
78       e.printStackTrace();
79     }
80   }
81
82   //
83
// Implement ActionListener
84
//
85

86   /**
87    * Invoked when an action occurs.
88    */

89   public void actionPerformed(ActionEvent JavaDoc e) {
90     String JavaDoc command = e.getActionCommand();
91     if ("AC_EXIT".equals( command )) {
92       this.windowClosing( null );
93     } else if ("AC_SAVE".equals( command )) {
94       System.out.println( "Save" );
95     } else {
96       System.out.println( "Click" );
97     }
98   }
99
100   //
101
// Overwrite Superclass implementation
102
//
103

104   /**
105    * Invoked when the user attempts to close the window
106    * from the window's system menu. If the program does not
107    * explicitly hide or dispose the window while processing
108    * this event, the window close operation will be cancelled.
109    */

110   public void windowClosing(WindowEvent JavaDoc e) {
111     System.out.println( "Good Bye!" );
112     super.windowClosing(e);
113     System.exit( 0 );
114   }
115
116   //
117
// Make the class bootable
118
//
119

120   public static void main(String JavaDoc[] args) {
121     new Actions();
122   }
123 }
124
Popular Tags