Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

keyboard vs menu conflict on Java 7, Mac OS

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:

    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.
    Eat that pie! EAT IT! Now read this tiny ad. READ IT!
    Thread Boost feature
      Bookmark Topic Watch Topic
    • New Topic