Setting mnemonic and Keyboard accelerator for menu and menu items

In this example we are going to see how to set mnemonic and Keyboard accelerators for menus and menu items. This is very useful in applications with forms, text boxes, menus etc. because it enables users to pick and set the focus on a specific compoment using only keyboard shortcuts.

To set mnemonic and keyboard accelerators for menu items, one should follow these steps:

  • Create a new JFrame.
  • Create a new JMenu.
  • Use setMnemonic('M') to set a mnemonic for the menu. This makes all the menus and menu items accessible. Now every time you hit Alt + M the menu will be accessible.
  • Create a new JMenuItem.
  • Use setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.SHIFT_MASK) to set an accelerator for the menu item.
  • Create a new JLabel.
  • Use setDisplayedMnemonic to set a mnemonic on the label. The associated component will get the focus when the mnemonic is activated.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class MenuAccessibility {

    public static void main(String[] args) {

// Create frame with specific title

JFrame frame = new JFrame("Example Frame");


 * Create a container with a flow layout, which arranges its children

 * horizontally and center aligned. A container can also be created with

 * a specific layout using Panel(LayoutManager) constructor, e.g.

 * Panel(new FlowLayout(FlowLayout.RIGHT)) for right alignment


Panel panel = new Panel();

// Create a Menu

JMenu menu = new JMenu("Menu");

// Set a mnemonic for the menu. This makes all the menus and menu items accessible


// Create a menu item

JMenuItem menuItem = new JMenuItem("Item");

// Set an accelerator key for the menu item

menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.SHIFT_MASK));

// Add the item to the menu


// Create a MenuBar and add the menu

JMenuBar menuBar = new JMenuBar();


// Create a component to add to the panel; in this case a text field with sample text

Component nameField = new TextField("Enter your name");

// Create a component to add to the panel; in this case a label for the name text field

JLabel nameLabel = new JLabel("Name:");

// Set a mnemonic on the label. The associated component will get the focus when the mnemonic is activated


// make the association explicit


// Add label and field to the container



// Create a component to add to the frame; in this case an image button - change to where your image file is located

JButton button = new JButton(new ImageIcon("image.png"));

// The tool tip text, if set, serves as the accessible name for the button

button.setToolTipText("Button Name");

// If tool tip is being used for something else, set the accessible name.

button.getAccessibleContext().setAccessibleName("Button Name");

// Set mnemonic for the button


// Add the components to the frame; by default, the frame has a border layout


frame.add(panel, BorderLayout.NORTH);

frame.add(button, BorderLayout.SOUTH);

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);




This was an examplw on how to set mnemonic and Keyboard accelerator for menu and menu items.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

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

Inline Feedbacks
View all comments
Back to top button