4

In the Java Desktop Application template used by Netbeans, a menu bar is created with JMenuBar and JMenuItems.

How can I get that bar displayed at the top, where menu bars are displayed in MacOSX instead of in-window, like in Windows?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

3 Answers3

14

By adding something like this into your code:

if (System.getProperty("os.name").contains("Mac")) {
  System.setProperty("apple.laf.useScreenMenuBar", "true");
}
oold
  • 326
  • 1
  • 5
  • If I run my app with this argument: -Dapple.laf.useScreenMenuBar=true, the bar is shown correctly at the top, but if I do this System.setProperty("apple.laf.useScreenMenuBar", "true"); on the main method, it doesn't. Any ideas? – Pablo Fernandez Oct 31 '09 at 15:00
  • I am not sure. I would try to put it as the first code in main() method or System.setProperty("apple.laf.useScreenMenuBar", "true"); without if condition (is not met if os.name contains "mac" instead of "Mac" for example) It seems to work on my 10.5.8 Leopard. – oold Oct 31 '09 at 16:55
  • I found that it worked if I placed it just after creating my JFrame (very first thing in main) and before creating my JMenuBar or doing any other modifications to my JFrame. This was under OS X 10.8.2 (Mountain Lion). – ArtOfWarfare Nov 28 '12 at 02:18
  • The test for "is a Mac" is a bit fragile. A better approach is to test for the existance of the functionality you need - in this case whether an Apple Java 6-specific class exists or not. – Thorbjørn Ravn Andersen Jun 10 '13 at 07:24
  • That test sounds fragile too. What you should be doing is testing whether the Aqua look & feel is actually in use. If you set this system property when using Nimbus, your application will crash, so it's important only to set it when using Aqua. – Hakanai Jan 10 '14 at 02:00
  • Why is the `if` check even necessary? Won't the property simply be ignored if you're not running on a mac? – khelwood Aug 01 '18 at 10:35
4

I had the same issue, but I realized that the MenuBar needs to be added to the frame as:

frame.setJMenuBar(menuBar);

instead of: frame.add(jMenuBar); along with: System.setProperty("apple.laf.useScreenMenuBar", "true"); in the main method.

zooes
  • 1,280
  • 3
  • 15
  • 21
3

Note: This is outdated information - a more recent answer is needed.

Java applications look like traditional java applications even under OS X.

If you want a native look and feel, there are a few tweaks you have to do. This article series describes them.

http://www.oracle.com/technetwork/articles/javase/javatomac-140486.html http://www.oracle.com/technetwork/java/javatomac2-138389.html

This includes setting the Dock icon and text, and integrating with the Applications menu.

I believe that the OS X "wrap jar as an application" utility with XCode sets all these properties automatically.

Community
  • 1
  • 1
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Since I wrote the answer, Apple has disowned Java, and the Oracle provided Java 7 works differently. I have not worked with Java 7 under OS X. – Thorbjørn Ravn Andersen Jun 10 '13 at 07:23
  • I've updated the old sun.com link to new ones to the oracle website, but the article images are missing and they are from 2003... – bobndrew Oct 07 '14 at 07:48