Actions are a popular addition to Swing. An action allows a programmer to bundle a commonly used procedure and its bound properties (such as its name and an image to represent it) into a single class. This construct comes in handy if an application needs to call upon a particular function from multiple sources. For example, let’s say that a Swing programmer creates an action that saves data to disk. The application could then invoke this action from both the Save menu item on the File menu and the Save button on a toolbar. Both components reference the same action object, which saves the data. If the Save function is disabled for some reason, this property can be set in the action as well. The menu and toolbar objects are automatically notified that they can no longer save any data, and they can relay that information to the user.
Swing containers, such as
JToolBar, can each accept action objects
add( ) methods. When an
action is added, these containers automatically create a GUI
component, which the
add( ) method then
returns to you for customization. For example, a
JMenu or a
JPopupMenu creates and returns a
JMenuItem from an
Action while a
JToolBar creates and returns a
JButton. The action is then paired with the
newly created GUI component in two ways: the GUI component registers
for any property changes that might occur in the action object, while the action object ...