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:

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@SuppressWarnings("serial") public class SwingMenuAcl extends JFrame { public SwingMenuAcl(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menu and Add it to Menu bar JMenu mnuFruits = new JMenu("Fruits"); menubar.add(mnuFruits); //Sample 04: Create Menu Items JMenuItem miApple = new JMenuItem("Apple"); JMenuItem miCherry = new JMenuItem("Cherry"); JMenuItem miGrapes = new JMenuItem("Grapes"); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Sample 05: Create Sub Menu For Banana JMenu subMenuBanana = new JMenu("Banana"); JMenuItem submiBanana1 = new JMenuItem("Type I"); JMenuItem submiBanana2 = new JMenuItem("Type II"); JMenuItem submiBanana3 = new JMenuItem("Type III"); subMenuBanana.add(submiBanana1); subMenuBanana.add(submiBanana2); subMenuBanana.add(submiBanana3); //Sample 06: Add Menu Items to Menu mnuFruits.add(miApple); mnuFruits.add(subMenuBanana); mnuFruits.add(miCherry); mnuFruits.add(miGrapes); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//Sample 07: Show Message when Grapes and Banana II Clicked miGrapes.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingMenuAcl.this, "Grapes Clicked"); } }); submiBanana2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingMenuAcl.this, "Banana II 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.
1 2 3 4 5 6 |
//Sample 07: Set Mnemonics for Three Menu Items & One Menu mnuFruits.setMnemonic(KeyEvent.VK_F); miApple.setMnemonic(KeyEvent.VK_A); miCherry.setMnemonic(KeyEvent.VK_C); miGrapes.setMnemonic(KeyEvent.VK_G); subMenuBanana.setMnemonic(KeyEvent.VK_B); |
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.
1 2 3 4 5 6 7 |
//Sample 08: Set Accelerator for Grapes and Type II banana KeyStroke acclGrapes = KeyStroke.getKeyStroke( KeyEvent.VK_G, Event.CTRL_MASK); KeyStroke acclBananaII = KeyStroke.getKeyStroke( KeyEvent.VK_B, Event.SHIFT_MASK); miGrapes.setAccelerator(acclGrapes); submiBanana2.setAccelerator(acclBananaII); |
8. Youtube Demo
9. Code Reference
9.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { SwingMenuAcl frame = new SwingMenuAcl("Menu Mnemonics & Accelerators"); frame.setVisible(true); } } |
9.2 SwingMenuAcl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Event; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.KeyStroke; @SuppressWarnings("serial") public class SwingMenuAcl extends JFrame { public SwingMenuAcl(String title) { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 02: Create Menu Bar and add to JFrame JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); //Sample 03: Create Menu and Add it to Menu bar JMenu mnuFruits = new JMenu("Fruits"); menubar.add(mnuFruits); //Sample 04: Create Menu Items JMenuItem miApple = new JMenuItem("Apple"); JMenuItem miCherry = new JMenuItem("Cherry"); JMenuItem miGrapes = new JMenuItem("Grapes"); //Sample 05: Create Sub Menu For Banana JMenu subMenuBanana = new JMenu("Banana"); JMenuItem submiBanana1 = new JMenuItem("Type I"); JMenuItem submiBanana2 = new JMenuItem("Type II"); JMenuItem submiBanana3 = new JMenuItem("Type III"); subMenuBanana.add(submiBanana1); subMenuBanana.add(submiBanana2); subMenuBanana.add(submiBanana3); //Sample 06: Add Menu Items to Menu mnuFruits.add(miApple); mnuFruits.add(subMenuBanana); mnuFruits.add(miCherry); mnuFruits.add(miGrapes); //Sample 07: Show Message when Grapes and Banana II Clicked miGrapes.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingMenuAcl.this, "Grapes Clicked"); } }); submiBanana2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingMenuAcl.this, "Banana II Clicked"); } }); //Sample 07: Set Mnemonics for Three Menu Items & One Menu mnuFruits.setMnemonic(KeyEvent.VK_F); miApple.setMnemonic(KeyEvent.VK_A); miCherry.setMnemonic(KeyEvent.VK_C); miGrapes.setMnemonic(KeyEvent.VK_G); subMenuBanana.setMnemonic(KeyEvent.VK_B); //Sample 08: Set Accelerator for Grapes and Type II banana KeyStroke acclGrapes = KeyStroke.getKeyStroke( KeyEvent.VK_G, Event.CTRL_MASK); KeyStroke acclBananaII = KeyStroke.getKeyStroke( KeyEvent.VK_B, Event.SHIFT_MASK); miGrapes.setAccelerator(acclGrapes); submiBanana2.setAccelerator(acclBananaII); } } |
Categories: Swing
Tags: getKeyStroke, KeyEvent, KeyStroke, setAccelerator, setMnemonic