JMenuBar at the top in MacOSX
Asked Answered
B

3

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?

Bilinear answered 31/10, 2009 at 14:26 Comment(0)
W
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.

Whitneywhitson answered 31/10, 2009 at 14:43 Comment(2)
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.Lydie
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...Trine
T
14

By adding something like this into your code:

if (System.getProperty("os.name").contains("Mac")) {
  System.setProperty("apple.laf.useScreenMenuBar", "true");
}
Timber answered 31/10, 2009 at 14:32 Comment(6)
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?Bilinear
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.Timber
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).Evangelineevangelism
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.Lydie
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.Orb
Why is the if check even necessary? Won't the property simply be ignored if you're not running on a mac?Coble
D
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.

Dispossess answered 19/1, 2013 at 16:7 Comment(1)
As of Mojave in 2019, this answer is what was necessary for mePistole
W
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.

Whitneywhitson answered 31/10, 2009 at 14:43 Comment(2)
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.Lydie
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...Trine

© 2022 - 2024 — McMap. All rights reserved.