Java Reference
In-Depth Information
something so you know when it is called. Add the
FileAction
class as an inner class to
SketcherFrame
where the comment indicated.
The
SketcherFrame
class needs a data member of type
FileAction
for each menu item that you intend
to add to the File menu, so add the following statement to the
SketcherFrame
class definition where the
comment indicated:
// File actions
private FileAction newAction, openAction, closeAction,
saveAction, saveAsAction, printAction, exitAction;
private FileAction[] fileActions;
// File actions as an array
Directory "Sketcher 6 using Action objects"
Note the additional
exitAction
member for a menu item to exit Sketcher. The
fileActions
array al-
lows you to process all the menu actions in the in a loop when necessary.
Actions for Elements Menu Items
You can define an inner class for the Elements menu next:
// Inner class defining Action objects for Element type menu items
class TypeAction extends AbstractAction {
// Create action with just a name property
TypeAction(String name, int typeID) {
super(name);
this.typeID = typeID;
}
// Create action with a name and an accelerator
private TypeAction(String name,int typeID, char ch, int modifiers) {
this(name, typeID);
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);
}
}
public void actionPerformed(ActionEvent e) {
elementType = typeID;
}
private int typeID;