0

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

brian_d
  • 11,190
  • 5
  • 47
  • 72
  • Which Windows version are you using? My Firefox 3.6.10 on XP requires Alt, which is what all native apps do. And running your first code sample with JDK6 also requires Alt. – Geoffrey Zheng Oct 07 '10 at 01:39
  • I am using WinXP, FF 3.6.10 at home, and Windows 7, FF 3.6.10 at work. Both Firefox and my first example show File with the 'F' underlined at all times on both machines. – brian_d Oct 07 '10 at 02:09
  • Sorry I didn't understand by "mnemonics will always appear" you meant the underline is always displayed. See my answer. – Geoffrey Zheng Oct 07 '10 at 04:49

1 Answers1

2

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
  • 6,562
  • 2
  • 38
  • 47
  • 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 Oct 07 '10 at 13:11
  • 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 Oct 07 '10 at 16:48
  • 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 Oct 07 '10 at 17:12