0

My code:

fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
fileMenu.setDisplayedMnemonicIndex(0);

Javadocs for AbstractButton.setDisplayedMnemonicIndex() say that

Not all look and feels may support this.

I set my look and feel to UIManager.getSystemLookAndFeelClassName()

And I don't see underline on mnemonic even when the index is explicitely set by setDisplayedMnemonicIndex() - under Windows 7 (only when I press ALT).

It works however if I do not set the look & feel and leave just default java theme.

Is there a way to achieve this? Is it caused by the settings of Windows ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ps-aux
  • 11,627
  • 25
  • 81
  • 128

2 Answers2

8

You can change the behaviour that the underline only appears when pressing the Alt key by setting a property with the UIManager

UIManager.getDefaults().put("Button.showMnemonics", Boolean.TRUE);

I think you need to do this before you display your first Swing component, but I'm not sure.

1

An underline will appear when you press the ALT key. I think it's intended as a visual feedback for user, to let them know that the underlined characters are now mapped to the corresponding keys as shortcuts.

Anas
  • 106
  • 6
  • I would have posted this as comment, if I had the privilege. – Anas Sep 15 '13 at 10:13
  • 1
    @Anas I know it it displayed after I press `ALT`. Is this how all windows applications work? +1 so you can get privilege faster:) – ps-aux Sep 15 '13 at 10:19
  • @horse - YEAH exactly. It worked. That is the correct answer. Too bad I cannot mark it and set this question as solved. – ps-aux Sep 15 '13 at 10:20