In a a java menu bar, the setMnemonic() method doesn't take a char anymore, it takes an int. They say this is better. WTF? How am I supposed to show a character with an int? Is it the octal character codes?
java.awt.event.KeyEvent has static int fields to pass to setMnemonic that corresponds to keys. If I read properly, casting an alphanumeric ASCII character to an int will be equivalent, but for others the members of KeyEvent should be used.
Docs are your friend. I'd post links, but posting from the phone. The javax.swing.AbstractButton doc describes how to use this.
-Lee
I always look at the docs. Usually they help... But they didn't this time. I'll try again.
open = new JMenuItem("Open Command...");
open.setMnemonic(KeyEvent.VK_O);
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
fileMenu.add(open);
What happens? Does cmd+O do anything? What should happen when the menu item is selected? Maybe adding the additional code that deals with O is mucking things up?
-Lee
For kicks you might check to see if:
KeyEvent.VK_O == (int)'O'
Or
KeyEvent.VK_O == (int)'o'
That's more just for your own edification. You might also try passing (int)'O' to setMnemonic to see if that works.
-Lee
Edit: after reading the link you posted, it looks like setMnemonic is discouraged, and just the setAccelerator should do. If the item does nothing, how do you know if it was "pressed" or not?
//Create the menu bar.
menuBar = new JMenuBar();
int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
//Build the first menu.
fileMenu = new JMenu("File");
fileMenu.getAccessibleContext().setAccessibleDescription("File");
menuBar.add(fileMenu);
//a group of JMenuItems
open = new JMenuItem("Open Command...");
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, mask));
open.addActionListener(this);
fileMenu.add(open);
fileMenu.addSeparator();