Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
setMnemonic() - Setting Keyboard Mnemonics on Menus
This section provides a tutorial example on how to use setMnemonic() method to associate mnemonics to menus. Mnemonics allows user to interact with menus using keys on keyboard.
Keyboard mnemonics can also be used on menus listed in the menu bar. But they behave differently than mnemonics on menu items.
1. Assign difference mnemonics, representing different keys on the keyboard, to different menus in the menu bar.
2. Adding a menu listener to each menu.
3. The character in the menu text that matches the mnemonic will be underscored.
4. When a mnemonic key is pressed together with the <Alt> key, the menu that has the mnemonic matching the pressed key will fire a menu event.
Here is an example program I wrote to test the setMnemonic() method on menus in the menu bar:
/* JMenuSetMnemonicTest.java * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved. */ import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JMenuSetMnemonicTest implements MenuListener { JFrame myFrame = null; public static void main(String[] a) { (new JMenuSetMnemonicTest()).test(); } private void test() { myFrame = new JFrame("Menu Mnemonic Test"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setBounds(50,50,250,150); myFrame.setContentPane(new JDesktopPane()); JMenuBar myMenuBar = new JMenuBar(); JMenu myMenu = getFileMenu(); myMenuBar.add(myMenu); myMenu = getColorMenu(); myMenuBar.add(myMenu); myMenu = getOptionMenu(); myMenuBar.add(myMenu); myMenu = new JMenu("Help"); myMenu.setMnemonic(KeyEvent.VK_H); myMenu.addMenuListener(this); myMenuBar.add(myMenu); myFrame.setJMenuBar(myMenuBar); myFrame.setVisible(true); } private JMenu getFileMenu() { JMenu myMenu = new JMenu("File"); myMenu.setMnemonic(KeyEvent.VK_F); myMenu.addMenuListener(this); JMenuItem myItem = new JMenuItem("Open"); myMenu.add(myItem); myItem = new JMenuItem("Close"); myMenu.add(myItem); myMenu.addSeparator(); myItem = new JMenuItem("Exit"); myMenu.add(myItem); return myMenu; } private JMenu getColorMenu() { JMenu myMenu = new JMenu("Color"); ButtonGroup myGroup = new ButtonGroup(); myMenu.setMnemonic(KeyEvent.VK_C); myMenu.addMenuListener(this); JRadioButtonMenuItem myItem = new JRadioButtonMenuItem("Red"); myItem.setSelected(true); myGroup.add(myItem); myMenu.add(myItem); myItem = new JRadioButtonMenuItem("Green"); myGroup.add(myItem); myMenu.add(myItem); myItem = new JRadioButtonMenuItem("Blue"); myGroup.add(myItem); myMenu.add(myItem); return myMenu; } private JMenu getOptionMenu() { JMenu myMenu = new JMenu("Option"); myMenu.setMnemonic(KeyEvent.VK_O); myMenu.addMenuListener(this); JMenuItem myItem = new JMenuItem("Sound"); myMenu.add(myItem); myItem = new JMenuItem("Auto save"); myMenu.add(myItem); return myMenu; } public void menuSelected(MenuEvent e) { JMenu myMenu = (JMenu) e.getSource(); System.out.println("Menu Selected: "+myMenu.getText()); } public void menuDeselected(MenuEvent e) { JMenu myMenu = (JMenu) e.getSource(); System.out.println("Menu deselected: "+myMenu.getText()); } public void menuCanceled(MenuEvent e) { JMenu myMenu = (JMenu) e.getSource(); System.out.println("Menu canceled: "+myMenu.getText()); } }
If you run this example, you will see the frame window shows up with the menu bar like this:
If you press <Alt>-f, <Alt>-c, <Alt>-o and <Alt>-h, you will see some messages printed on the Java console window:
Menu Selected: File Menu deselected: File Menu Selected: Color Menu deselected: Color Menu Selected: Option Menu deselected: Option Menu Selected: Help Menu deselected: Help
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
JCheckBox - Swing Check Box Class
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
JComboBox - Swing Combo Box Class
►Menu Bar, Menus, Menu Items and Listeners
JMenuBar, JMenu, and JMenuItem Classes
JMenuBarTest.java - Menu Bar Test Program
JMenuTest.java - Menu Test Program
JMenuItemTest.java - Menu Item Test Program
JRadioButtonMenuItemTest.java - Radio Button Menu Item Test Program
JCheckBoxMenuItemTest.java - Check Box Menu Item Test Program
javax.swing.event.MenuListener - Menu Listener Interface
JMenuItemActionListenerTest.java - Menu Item Action Listener Test
Item Listener on Radio Button Menu Items
Item Listener on Check Box Menu Items
javax.swing.event.MenuKeyListener - Menu Key Listener Interface
setMnemonic() - Setting Keyboard Mnemonics on Menu Items
setAccelerator() - Setting Keyboard Accelerators on Menu Items
►setMnemonic() - Setting Keyboard Mnemonics on Menus
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
AWT (Abstract Windows Toolkit)