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 because
they give an experienced user the ability to select menu items rapidly.
Keyboard shortcuts come in two forms: mnemonics and accelerators. 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
is a key that lets you select a menu item without having to first activate the
menu.
A mnemonic can be specified
for both JMenuItem and JMenu objects. There are two ways to
set the mnemonic for JMenuItem.
First, it can be specified when an object is constructed using this
constructor:
JMenuItem(String name, int mnem)
In this case, the name is
passed in name and the mnemonic is
passed in mnen. Second, you can set
the mnemonic by calling setMnemonic( ).
To specify a mnemonic for JMenu, you
must call setMnemonic( ). This
method is inherited by both classes from AbstractButton
and is shown next:
void setMnemonic(int mnem)
Here, mnem specifies the mnemonic. It should be one of the constants
defined in java.awt.event.KeyEvent,
such as KeyEvent.VK_F or KeyEvent.VK_Z. (There is another version of setMnemonic( ) that takes a char
argument, but it is considered obsolete.) Mnemonics are not case sensitive, so
in the case of VK_A, typing either a or A
will work.
By default, the first
matching letter in the menu item will be underscored. In cases in which you
want to underscore a letter other than the first match, specify the index of
the letter as an argument to setDisplayedMnemonicIndex(
), which is inherited by both JMenu
and JMenuItem from AbstractButton. It is shown here:
void
setDisplayedMnemonicIndex(int idx)
The index of the letter to
underscore is specified by idx.
An accelerator can be
associated with a JMenuItem object.
It is specified by calling setAccelerator(
), shown next:
void setAccelerator(KeyStroke
ks)
Here, ks is the key combination that is pressed to select the menu item. KeyStroke is a class that contains
several factory methods that construct various types of keystroke accelerators.
The following are three examples:
static KeyStroke
getKeyStroke(char ch)
static KeyStroke
getKeyStroke(Character ch, int modifier) static KeyStroke
getKeyStroke(int ch, int modifier)
Here, ch specifies the accelerator character. In the first version, the
character is specified as a char
value. In the second, it is specified as an object of type Character. In the third, it is a value of type KeyEvent, previously described. The value of modifier must be one or more of the following constants, defined in
the java.awt.event.InputEvent class:
InputEvent.ALT_DOWN_MASK
InputEvent.CTRL_DOWN_MASK
InputEvent.SHIFT_DOWN_MASK
InputEvent.ALT_GRAPH_DOWN_MASK
InputEvent.META_DOWN_MASK
Therefore, if you pass VK_A for the key character and InputEvent.CTRL_DOWN_MASK for the
modifier, the accelerator key combination is ctrl-a.
The following sequence adds
both mnemonics and accelerators to the File menu created by the MenuDemo program in the previous
section. After making this change, you can select the File menu by typing
alt-f. Then, you can use the mnemonics o, c, s, or e to select an option.
Alternatively, you can directly select a File menu option by pressing ctrl-o,
ctrl-c, ctrl-s, or ctrl-e. Figure 33-2 shows how this menu looks when
activated.
// Create the File menu with mnemonics and
accelerators. JMenu jmFile = new JMenu("File");
jmFile.setMnemonic(KeyEvent.VK_F);
JMenuItem jmiOpen = new
JMenuItem("Open", KeyEvent.VK_O);
jmiOpen.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_DOWN_MASK));
JMenuItem jmiClose = new
JMenuItem("Close", KeyEvent.VK_C);
jmiClose.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_DOWN_MASK));
JMenuItem jmiSave = new
JMenuItem("Save", KeyEvent.VK_S);
jmiSave.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_DOWN_MASK));
JMenuItem jmiExit = new
JMenuItem("Exit", KeyEvent.VK_E);
jmiExit.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_E,
InputEvent.CTRL_DOWN_MASK));
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.