Is there a way to set underline to mnemonic character in native look and feel under Win 7?

2.8k views Asked by At

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 ?

2

There are 2 answers

0
AudioBubble On BEST ANSWER

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.

3
Anas On

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.