Java Reference
In-Depth Information
openItem.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL _ MASK ));
saveItem.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL _ MASK ));
printItem.setAccelerator(KeyStroke.getKeyStroke('P',Event.CTRL _ MASK ));
// Construct the Element pull down menu as before...
// Add element type accelerators
lineItem.setAccelerator(KeyStroke.getKeyStroke('L',Event.CTRL _ MASK ));
rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E',Event.CTRL _ MASK ));
circleItem.setAccelerator(KeyStroke.getKeyStroke('I',Event.CTRL _ MASK ));
curveItem.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL _ MASK ));
elementMenu.addSeparator();
// Create the color submenu as before...
// Add element color accelerators
redItem.setAccelerator(KeyStroke.getKeyStroke('R',Event.CTRL _ MASK ));
yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y',Event.CTRL _ MASK ));
greenItem.setAccelerator(KeyStroke.getKeyStroke('G',Event.CTRL _ MASK ));
blueItem.setAccelerator(KeyStroke.getKeyStroke('B',Event.CTRL _ MASK ));
menuBar.add(fileMenu); // Add the file menu
menuBar.add(elementMenu); // Add the element menu
}
// File menu items and the rest of the class as before...
}
If you save SketchFrame.java after you have made the changes, you can recompile Sketcher and
run it again. The file menu will now appear as show below:
How It Works
We use the setMnemonic() method to set the shortcuts for the menu bar items and the
setAccelerator() method to add accelerators to the submenu items. You must make sure that you
do not use duplicate key combinations, and the more menu items you have accelerators for, the trickier
this gets. The File menu here defines the standard Windows accelerators. You can see that the
setAccelerator() method adds the shortcut key combination to the item label.
Search WWH ::




Custom Search