Add Mnemonics and Accelerators to Menu Items
The menu created in the preceding example is functional, but it is possible to make it better. In real applications, a menu usually includes support for keyboard shortcuts. These come in two forms: accelerators and mnemonics. An accelerator is a key combination that lets you select a menu item without having to first activate the menu. As it applies to menus, a mnemonic defines a key that lets you select an item from an active menu by typing the key. Thus, a mnemonic allows you to use the keyboard to select an item from a menu that is already being displayed.
An accelerator can be associated with a Menu or MenuItem. It is specified by calling setAccelerator( ), shown next:
final void setAccelerator(KeyCombination keyComb)
Here, keyComb is the key combination that is pressed to select the menu item. KeyCombination is class that encapsulates a key combination, such as ctrl-s. It is packaged in javafx.scene.input.
KeyCombination defines two protected constructors, but often you will use the keyCombination( ) factory method, shown here:
static KeyCombination keyCombination(String keys)
In this case, keys is a string that specifies the key combination. It typically consists of a modifier, such as ctrl, alt, shift, or meta, and a letter, such as s. There is a special value, called shortcut, which can be used to specify the ctrl key in a Windows system and the meta key on a Mac. (It also maps to the typically used shortcut key on other types of systems.) Therefore, if you want to specify ctrl-s as the key combination for Save, then use the string "shortcut+S". This way, it will work for both Windows and Mac and elsewhere.
The following sequence adds accelerators to the File menu created by the MenuDemo program in the previous section. After making this change, you can directly select a File menu option by pressing ctrl-o, ctrl-c, ctrl-s, or ctrl-e.
// Add keyboard accelerators for the File menu.
open.setAccelerator(KeyCombination.keyCombination("shortcut+O")); close.setAccelerator(KeyCombination.keyCombination("shortcut+C")); save.setAccelerator(KeyCombination.keyCombination("shortcut+S")); exit.setAccelerator(KeyCombination.keyCombination("shortcut+E"));
A mnemonic can be specified for both MenuItem and Menu objects, and it is very easy to do. Simply precede the letter in the name of the menu or menu item with an underscore. For example, in the preceding example, to add the mnemonic F to the File menu, declare fileMenu as shown here:
Menu fileMenu = new Menu("_File"); // now defines a mnemonic
After making this change, you can select the File menu by typing alt then f. However, mnemonics are active only if mnemonic parsing is true (as it is by default). You can turn mnemonic parsing on or off by using setMnemonicParsing( ), shown here:
final void setMnemonicParsing(boolean enable)
In this case, if enable is true, then mnemonic parsing is turned on. Otherwise, it is turned off.
After making these changes, the File menu will now look like this:
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.