aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes keyboard vs menu conflict on Java 7, Mac OS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "keyboard vs menu conflict on Java 7, Mac OS" Watch "keyboard vs menu conflict on Java 7, Mac OS" New topic
Author

keyboard vs menu conflict on Java 7, Mac OS

J. Clarke
Greenhorn

Joined: May 14, 2012
Posts: 2
How can I get a Java 7 application to have its menu bar at the top of the screen (on a Mac) and also have correctly working keyboard shortcuts?


I have a Java application with a Swing user interface. Many menus have keyboard equivalents, which are essential.

There is very little that is system-dependent, but on Mac OS X the menu bar should appear at the top of the screen instead of on each window, so I set apple.laf.useScreenMenuBar.

This works fine on Java 6, but on Java 7 compiling and running the same code causes the keyboard shortcuts to carry out their menu actions twice. For example, in the attached code, command-O opens two file dialogues instead of one. (The other keyboard shortcuts also act twice, but you sometimes have to move windows to see that they did.)

The keyboard problem goes away if I don't set apple.laf.useScreenMenuBar, and that's what I'll do if I have to, but my Mac users will be displeased. I'd really like to have the menu bar in the right place and the keyboard shortcuts working.

System: Mac OS 10.7.3 (Lion) on a late-2010 MacBook Pro

Java 7:
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

Java 6:
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-11M3635)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01-415, mixed mode)

Where I've looked:
  • I asked the same question on StackOverflow, without (so far) any responses. If you can answer here, I'll be happy to write up your answer there, unless you'd prefer to do that yourself.
  • A discussion of why apple.laf.useScreenMenuBar should be gotten rid of -- I'm all for it, but it doesn't seem to have happened.
  • A discussion about not using mrj.version to detect that you're on a Mac -- not directly relevant, but sounded promising.

  • My apologies for the length of the demo code below (148 lines), but my Swing coding is very old-fashioned. It should compile and run from the command line without any special flags or settings. Here we go ...

    J. Clarke
    Greenhorn

    Joined: May 14, 2012
    Posts: 2
    Update: Keyboard shortcuts now work ... but they are not listed in menus! I discovered this while trying the new 7u7 release, but the new bug seems to have been reported already for 7u6: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7186371

    I'd also updated to Mountain Lion (Mac OS X 10.8.1), but a quick check on a Lion system seems to show that the "improvement" is because of the Java release, not the OS change.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: keyboard vs menu conflict on Java 7, Mac OS
     
    Similar Threads
    help with GUI practice
    mnemonics are not getting focus for the menubar
    JCheckboxMenuItem won't change selection color
    JTextArea append() not working?
    Adding Components in to JInternalFrame