views:

69

answers:

1

In the NetBeans 6.9 IDE, if you create a New Project >> Java >> Java Desktop Application and run it, you will find that the menu items have mnemonics, but only after ALT is pressed.
(The netbeans program itself uses this style of menu.)

However, if you create a new File >> Swing GUI Forms >> JFrame Form, and add a simple menubar with mnemonics, then run the JFrame, the mnemonics will always appear without having to press ALT. This is what I would prefer.
(Firefox uses this style of menu)

My thoughts are that the org.jdesktop.application overrides the default setting, but that's just a guess. Anyone know how to make a SingleFrameApplication not require ALT to be pressed?

Thanks.

Edit: The problem was found to be that JFrame and JDesktop use different default Look and Feels

+2  A: 

It's a Windows setting. In XP go to:

  1. Control Panel
  2. Display
  3. Appearance
  4. Effects
  5. Hide underlined letters for keyboard navigation until I press the Alt key

(Win7 should have a similar setting somewhere, I suppose.)

The default setting is on, so Java is right and Firefox is wrong (even Office 2003 doesn't respect that setting).

Uncheck it and you'll always see mnemonic underline in Java.

Note that only Windows LAF correctly respects the setting. Motif and Metal always show the underline. I don't use NetBeans or jDesktop but I guess it uses system LAF and thus the underline is correct.

If you still want to always show underline under Windows LAF (please think twice before you do), call UIManager.getLookAndFeelDefaults().put("Button.showMnemonics", false), which does NOT seem to work for XP because WindowsMenuItemUI#paintText only checks the flag under Vista. You could check Win7 JDK yourself.


Note that there's an accepted bug when the setting is on, which goes like this (saving you some time to parse the 2nd most awful bug tracking system in the universe. The worst is an in-house ColdFusion system my company used to have): create one menu with mnemonic, for example &File, press Alt-F, release, press Alt-F again, the underline is gone. They are back as soon as you do anything else, clicking, or just press Alt by itself.

Geoffrey Zheng
I forgot to deal with the jdesktop part, but as you can see from my answer, whatever it does it's probably not right, since you should respect platform settings. And BTW motif and synth lafs always show the underline. I hope nimbus does it right.
Geoffrey Zheng
Thanks for the info. In Windows 7, the same setting is`Control Panel >> All Control Panel Items >> Ease of Access Center` `>> Make the keyboard easier to use >>` `(Checkbox) "Underline keyboard shortcuts and access keys"`So always showing the mnemonics is the exception, rather than the norm.However the question of "Why does a basic JFrame always show the underline while a JDesktop does not?" remains.Both have the metal LAF, and my "underline keyboard shortcuts" setting is unchecked. What can I do to the JDesktop to accomplish this? Is it as simple as a `UIManager` setting?
brian_d
Sorry I meant metal LAF in my previous comment. It does NOT respect the Windows setting. jDesktop is doing it right (another brainfart in my previous comment) to respect the setting. See my updated answer.
Geoffrey Zheng
You are correct! `UIManager.getLookAndFeelDefaults().put("Button.showMnemonics", true)` does the trick. I was erroneous in reporting that both JFrame and JDesktop were using the metal LAF. (I ran `System.out.println(UIManager.getLookAndFeel());` before the constructor call instead of after it.) Thanks a lot!
brian_d