Swing Actions

Swing introduced the Action interface and classes so that...

« Interface »
ActionListener
+ actionPerformed(ActionEvent)
« Interface »
Action
+ addPropertyChangeListener(Listener)
+ getValue(String) : Object
+ putValue(String, Object)
+ isEnabled() : boolean
+ setEnabled(boolean)
« abstract »
AbstractAction
+ addPropertyChangeListener(Listener)
+ getValue(String) : Object
+ putValue(String, Object)
+ isEnabled() : boolean
+ setEnabled(boolean)
« abstract »
+ actionPerformed(ActionEvent)

...menu items and toolbar buttons could share the same action.

 public class MyFrame  {

    public MyFrame() {
        JMenu    fileMenu = new JMenu("File");
        JToolBar toolBar  = new JToolBar();
    
        Action openAction = new AbstractAction("Open") {
            public void actionPerformed(ActionEvent event) {
                doOpenFile();
            }
        };
    
        fileMenu.add(new JMenuItem(openAction));
        toolBar.add(new JButton(openAction));