Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JMenu Mnemonic & Accelerator

1. Introduction to Mnemonic & Accelerator

A JMenuItem can support both Mnemonic and Accelerator. Menu Mnemonic are useful for accessing the menu item using the keyboard Key Char and alt key combination. Mnemonic will animate the menu and MenuItem selection while corresponding key stroke rises from the keyboard. With Mnemonic, we can access the menu commands without using the mouse. Accelerators are usually combined with ctrl + key or shift + key or some times with ctrl + shift key. Unlike Mnemonic, the accelerator does not produce any menu access animation. It directly invokes the command handler.

2. About the Mnemonic & Accelerator Example

The example which we create in this article is below:

JMenu Mnemonic and Accelerator
JMenu Mnemonic and Accelerator

The Example has a single JMenu (Fruits) in the menu bar. The Fruits menu contains 4 menu items in which the second menu will invoke a sub-menu. Three items are in the sub-menu. The JMenu fruits is having an underline below the letter F. The same way all its four menu items have an underline in it. For example, to invoke Apple, the user will hit Alt+F+A. The Menu Item Grapes and the menu item Type II contains greyed letters which denote Ctrl+G and Shift+B. For example, a user can invoke the menu item Type II using the keystrokes CTRL & B. Underline denotes Mnemonic and Greyed out letters denotes menu accelerators. Now we will proceed with creating this example.

3. Prepare Main Menu

First, we prepare the JFrame window with the border layout manager. Then, code snippets 2 and 3 create Fruits menu for the JMenuBar. Finally, in code snippet 4, we create three menu items for this Fruits menu.

4. Create Sub-Menu

A JMenu which is not attached to a JMenuBar but attached to one more JMenu is called Sub-menu. In the below code, we create a JMenu for Banana. This Menu houses three JMenuItem objects via add method call. If we attach this to the JMenuBar, it will appear as one more menu next to the Fruits. But in code snippet 6, Line 12, we add this JMenu (Banana) to one more JMenu (Fruits). Now, Banana is the sub-menu under the Fruits Menu.

5. Handle Menu Action

We handle only two menu items’ click handler and this is to check how mnemonic and accelerator works. In both the handler functions we display a message box stating, menu item is clicked.

6. Set Swing Menu Mnemonic

We can set the mnemonic using the setMnemonic function. In the below code, we used this method on the JMenu and JMenuItem objects. The method expects a constant from KeyEvent so that it can set a mnemonic. Recall, we already set display text for the menu items and menus. Now, we need to pick a letter from that as a menu mnemonic.

7. Set Menu Accelerator via KeyStroke

One can use the KeyStroke class to represent a Char Key in combination with control keys. Example for Char Keys are A, a, B, b etc. Examples for control keys are shift, alt, ctrl. The KeyStroke class exposes a static method called getKeyStroke and using this we can get the Char + Control key combination. In the below code, we create key combinations: 1) CTRL+G and 2) SHIFT+B. Then, using the setAccelerator method, we set this key combination as the accelerator for the menu items Grapes and Sub-menu item Banana. Note, you can combine more than one control key as well by passing the second param like: Event.SHIFT_MASK + Event.CTRL_MASK.

8. Youtube Demo

Demo: Menu Accelerator and Mnemonic

9. Code Reference



Categories: Swing

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.