Java Reference
In-Depth Information
can add the very same
Action
object to both a menu and a toolbar, and because the
Action
object is its
own listener, you automatically get both the menu item and the toolbar button supported by the same action.
Clever, eh?
There's more. You can construct any type of menu item, including those of type
JCheckBoxMenuItem
and
JRadioButtonMenuItem
, from
Action
objects, so if you want to have these on your menus, you can.
You just add the menu items you create from your
Action
objects to a menu in the way you have seen. First,
you need to 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 cor-
responding 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 several ba-
sic standard properties that relate to an
Action
object. These properties are accessed through keys of type
String.
The identifiers for the keys that are defined in the
Action
interface and the corresponding values
are the following:
•
NAME
: A
String
object that is the label for a menu item or a toolbar button.
•
SMALL_ICON
: A
javax.Swing.Icon
reference to a small icon for a menu item (typically a
javax.Swing.ImageIcon
object).
•
LARGE_ICON_KEY
: An
Icon
reference to a large icon for a toolbar button (typically an
ImageIcon
object).
•
SHORT_DESCRIPTION
: A
String
object that is a tooltip.
•
LONG_DESCRIPTION
: A
String
object that is a context-sensitive help message.
•
ACCELERATOR_KEY
: A
javax.swing.KeyStroke
object that represents a key combination is the
accelerator for the action.
•
DISPLAYED_MNEMONIC_INDEX_KEY
: An
Integer
object indicating where in the
NAME
property
value the decoration (which is an underline) for a mnemonic should be rendered. You use this to
create a visual cue to the character for the accelerator.
•
MNEMONIC_KEY
: An
Integer
that corresponds to a
KeyEvent
key code.
•
SELECTED_KEY
: A
Boolean
object that records the selected or unselected state.
•
ACTION_COMMAND_KEY
: A
String
object that is the command string for the action.
Just so you are aware of them I have included the complete set here, but you will only use a few of them
in Sketcher. You are not obliged to provide for all of these properties in your action classes, but the
Action
interface provides the framework for it. There is another key defined in the interface with the name
DEFAULT
,
but this is not used currently.
Action Methods
The
Action
interface also declares the following methods:
•
void putValue(String key,Object value)
: Stores the object
value
associated with the key
constant
key
in the action object. To store the name of an action within a class method, you might
write: