Java : Adding actions to components

Created 06 December 2002 00:00
Actions can be very useful when two objects must perform the same action. For example, if you want a menu item and a button to do the same thing, use an action as follows:
  Action act = new AbstractAction("Does something") {
    public void actionPerformed(ActionEvent ae) {
      System.out.println("Something happened!");
    }
  };
  JMenuItem item = new JMenuItem(act);
  JButton btn = new JButton(act);