Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top