Motivation (cont’d)
Often, we might like to have a single action handle events fired from multiple components. The most common scenario is the menu bar mapped to button events.
Swing containers can accept ‘action objects’ with their add() method, allowing them to automatically create a GUI component, returned to you upon calling add().
JMenu menu = new JMenu();
JMenuItem item= menu.add(action);
JToolBar toolbar = new JToolBar();
JButton button = toolbar.add(action);