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:
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();
}
}
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.