Java Reference
In-Depth Information
The setMnemonic() method in lines 41, 46, 54, 59, 66, 71, 78, and 85 sets
the keyboard mnemonic for a Menu object or a MenuItem object. A mnemonic
is a device used as an aid in remembering. The mnemonic key is the keyboard
letter that — when combined with another key, such as ALT — will activate the
object if the window has the focus. A mnemonic must correspond to a single key
on the keyboard and should be specified using one of the VK keycodes. Java uses
a VK keycode , or virtual key keycode, to determine which keyboard key has
been pressed. VK keycodes belong to the KeyEvent class , a class of methods
associated with keyboard events such as pressing a key, releasing a key, or typing
into a text box. The general format of the setMnemonic() method is as follows.
object.setMnemonic(KeyEvent.keycode);
Java supports a VK keycode for each of the letter numbers, and function keys
on a standard keyboard by using the syntax, VK_, followed by the appropriate
key. For example, to set the mnemonic to the letter F in a File menu, the object
would be the name of the menu and the keycode would be VK_F.
mnuFile.setMnemonic(KeyEvent.VK_F);
Mnemonics are not case sensitive; therefore, a KeyEvent with the corres-
ponding keycode would cause the button to be activated whether or not the
SHIFT key was pressed.
Java uses a second method involving mnemonics to specifically assign a
visual underline to a certain letter in a menu or label for use as a shortcut key.
The setDisplayedMnemonicIndex() method in lines 42, 47, 55, 60, 67, 72, 79,
and 86 assigns the shortcut key to a letter in the command's keyword, underlin-
ing the letter in the menu or label. Java indexes the keyword characters begin-
ning with zero and progressing sequentially. Therefore, in line 86, setting the
mnemonic index to 3 will assign the fourth element of the Menu's caption string
— in this case, the letter Y — as the shortcut. Thus, the second occurrence of the
letter Y in the caption, by Year, will display underlined. Pressing A LT + Y will trig-
ger the command in the same way as clicking the menu command. Mnemonics
are assigned to each of the menu system commands to use as shortcuts in the
same way.
Setting a Shortcut Key
Use the setDisplayedMnemonicIndex() method to assign a visual
underline to a specific letter in a menu or label for use as a
shortcut key.
The setActionCommand() method in lines 49, 62, 74, 81, and 88 assigns a
String that can be evaluated in the actionPerformed() method. An if statement
will match the assigned action with the action of the clicked object; in this way, it
is able to determine which command was clicked. For example, in line 62, the
setActionCommand() method sets a String to the word, Insert, which then will
be evaluated later in the actionPerformed() method.
Search WWH ::




Custom Search