Java Reference
In-Depth Information
The AbstractAction class definition already provides the mechanism for storing action properties. For
the last two constructors, the argument values are stored using the standard keys in the Action interface that
I described earlier. For the moment, you use the constructor with just a name argument and leave icons until
a little later.
Actions for File Menu Items
You can define the FileAction inner class as follows:
// Inner class defining Action objects for File menu items
class FileAction extends AbstractAction {
// Create action with a name
FileAction(String name) {
super(name);
}
// Create action with a name and accelerator
FileAction(String name, char ch, int modifiers) {
super(name);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));
// Now find the character to underline
int index = name.toUpperCase().indexOf(ch);
if(index != -1) {
putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);
}
}
// Event handler
public void actionPerformed(ActionEvent e) {
// You will add action code here eventually...
}
}
Directory "Sketcher 6 using Action objects"
You have two constructors. The first just stores the name for the action as the NAME property by calling
the base class constructor. The second stores the name by calling the first constructor and then creates
and stores the accelerator keystroke in the Action object as the value for the ACCELERATOR_KEY property.
You create the KeyStroke object from the ch and modifiers arguments. The character, ch , must always
be an uppercase letter because lowercase letters are used to identify non-alphabetic keys in a keystroke.
You find the index of the character in the name that corresponds to ch and use that as the value for the
DISPLAYED_MNEMONIC_INDEX_KEY property. This causes the character at that index position in the name to
be underlined.
Because the class is an action listener, you have implemented the actionPerformed() method in it. You
don't yet know what you are going to do with the File menu item actions, so you can leave it open for
now and let the actionPerformed() method do nothing. Alternatively you could add a statement to output
Search WWH ::




Custom Search