Miles D
Miles D

Reputation: 8060

Java Menu Mnemonics in Resource Files

I would like to assign a mnemonic to a JMenu using resource bundles (or the ResourceMap). So for example, the code without resource file would be...

JMenu fileMenu = new JMenu();
fileMenu.setText("File");   // this would be read from a resource file
fileMenu.setMnemonic('F');  // but the docs say this is obsolete
fileMenu.setMnemonic(KeyEvent.VK_F);

So how do I put the KeyEvent.VK_F in a resource file?

For a JMenuItem I can do it with actions, but this is JMenu.

Upvotes: 9

Views: 1904

Answers (3)

Michael Brewer-Davis
Michael Brewer-Davis

Reputation: 14296

Java's javax.swing.KeyStroke class bridges the gap:

JMenu fileMenu = new JMenu();
String mnemonic = // string from localization
fileMenu.setMnemonic(KeyStroke.getKeyStroke(mnemonic).getKeyCode());

Accelerators are not supported for JMenus, only for JMenuItems (which makes sense, since these invoke an action without using the menu at all).

Upvotes: 12

ShawnD
ShawnD

Reputation: 992

Inside the resource file use the accelerator

add.Action.accelerator = control A

Upvotes: 1

John Gardner
John Gardner

Reputation: 25178

You could do it in a similar way, and treat "FileMenu" as a (fake) action?

Upvotes: 0

Related Questions