Home » Desktop Java » accessibility » Setting mnemonic and Keyboard accelerator for menu and menu items

About Byron Kiourtzoglou

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.

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

menu.setMnemonic('M');

// 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

menu.add(menuItem);

// Create a MenuBar and add the menu

JMenuBar menuBar = new JMenuBar();

menuBar.add(menu);

// 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

nameLabel.setDisplayedMnemonic('N');

// make the association explicit

nameLabel.setLabelFor(nameField);

// Add label and field to the container

panel.add(nameLabel);

panel.add(nameField);

// 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

button.setMnemonic('B');

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

frame.setJMenuBar(menuBar);

frame.add(panel, BorderLayout.NORTH);

frame.add(button, BorderLayout.SOUTH);

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

    }

}

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

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of