Skip to Page Content | Navigation for Module


Navigation for Module 10: Scripts/Java
Page 14 of 19

Mnemonics and Accelerators in Java

Individuals who are deaf, or who have motor skill or visual impairments, often rely on alternative input devices, like the keyboard. Therefore, it cannot be assumed that the end user operates using a mouse. Topics such as tabbing order and hotkeys should be incorporated into the original application design.

The overall interaction and functional layout is usually defined by the menu bar, so making sure it is accessible should be a top priority. Java offers two different kinds of keyboard alternatives:

Note: Proficiency with using accelerators actually increases efficiency for common operations by eliminating interaction with the mouse. While keyboard interaction may be the only option for individuals with disabilities, notice once again that designing for accessibility benefits all users.

Code example of using mnemonics at both the menu and menu item level:

import javax.swing.*;
import java.awt.event.*;
 
class AccessibleMenuTest extends JFrame implements ActionListener{
   JButton button1;
   JMenuBar menubar;
   JMenu file, search, help;
   JMenuItem open, save, exit;
 
   AccessibleMenuTest() {
      super ("Accessible Menu Test");
 
      // To set the mnemonics of menu items, you can either pass the
      // key in the constructor, or use the setMnemonic() method
      open = new JMenuItem ("Open", KeyEvent.VK_O);
      save = new JMenuItem("Save", KeyEvent.VK_S);
      save.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
      save.addActionListener (this);
      exit = new JMenuItem("Exit");
      exit.setMnemonic(KeyEvent.VK_E);
 
      // Set up the menus in a similar fashion, but don't forget about
      // setting the descriptions as well!
      file = new JMenu ("File");
      file.setMnemonic (KeyEvent.VK_F);
      file.getAccessibleContext().setAccessibleDescription("File and Program menu");
      search = new JMenu ("Search");
      search.setMnemonic (KeyEvent.VK_S);
      search.getAccessibleContext().setAccessibleDescription("Search and Editing menu");
      help = new JMenu ("Help");
      help.setMnemonic (KeyEvent.VK_H);
      help.getAccessibleContext().setAccessibleDescription("Help and About menu");
 
      // Finish building the menu bar
      file.add (open);
      file.add (save);
      file.add (exit);
 
      menubar = new JMenuBar();
      menubar.add(file);
      menubar.add(search);
      menubar.add(help);
 
      this.setJMenuBar(menubar);
 
      this.setSize(400,75);
      this.setVisible(true);
   }
   public void actionPerformed (ActionEvent e) {
      System.out.println ("Action called");
   }
   public static void main (String args[]) {
      AccessibleMenuTest amt = new AccessibleMenuTest();
   }
}

Note: When creating a JMenuItem, you have the option of passing the mnemonic key, or you can make an explicit call to the setMnemonic( ) method. Also, in this example, the key combination CTRL-S has been bound to the save menu item, effectively eliminating the need to go through the menu hierarchy. Furthermore, the setting of the descriptions. Technically, all visible components need to have descriptions, but they were intentionally left out for the menu items for brevity.

The next page will discuss the image icons.

Top of Page arrow up
       Page 14


 
-- END OF PAGE