Java Reference
In-Depth Information
colors.add(greenItem);
colors.add(blueItem);
Now our Color menu checks are set automatically so we can forget about them.
Using Actions
One difficulty with the code that we have added to support the menus is that it is very menu specific.
What I mean by this is that if we are going to do a proper job on the Sketcher application, we will
undoubtedly want it to have a toolbar. The toolbar will surely have a whole bunch of buttons that
perform exactly the same actions as the menu items we have just implemented, so we will be in the
business of doing the same thing over again in the toolbar context. Of course, the only reason I brought
it up, as I'm sure you anticipated, is that there is another way of working with menus, and that is to use
an action object.
An action object is a bit of a strange beast, and it can be quite hard to understand at first so we will take
it slowly. First of all let's look at what we mean by an 'action' here, as it is a precise term in this context.
An action is an object of any class that implements the Action interface. This interface declares
methods that operate on an action object, for example storing properties relating to the action, enabling
it and disabling it. The Action interface happens to extend the ActionListener interface so an
action object is a listener as well as an action. Now that we know an Action object can get and set
properties, and is also a listener, how does that help us in implementing the Sketcher GUI?
The answer is in the last capability of an Action object. Some Swing components, such as those of type
JMenu and JToolBar , have an add() method that accepts an argument of type Action . When you add
an Action object to these using the add() method, the method creates a component from the Action
object that is automatically of the right type . If you add an Action object to a JMenu object, a JMenuItem will
be created and returned by the add() method. On the other hand, when you add exactly the same Action
object to a JToolBar object, an object of type JButton will be created and returned. This means that you
can add the very same Action object to both a menu and a toolbar, and since the Action object is its own
listener you automatically get both supporting the same action. Clever, eh?
First, we should look at the Action interface.
The Action Interface
In general, properties are items of information that relate to a particular object and are stored as part of
the object. Properties are often stored in a map, where a key identifies a particular property, and the
value corresponding to that property can be stored in association with the key. The Properties class
that is defined in the java.util package does exactly that. The Action interface has provision for
storing seven basic standard properties that relate to an Action object:
A name - a String object that is used as the label for a menu item or a toolbar button.
A small icon - an Icon object to be displayed on a toolbar button.
A short description of the action - a String object to be used as a tooltip.
An accelerator key for the action - defined by a KeyStroke object.
Search WWH ::




Custom Search