Java Mac menubar - How to put your Java application name on the Mac menu bar

Java Mac menubar FAQ: A frequently asked question regarding Java Swing applications running on Mac OS X is "How do I put my Java (Swing) application name on the Mac menu bar?"

Update: You have to do some other things besides what’s shown in this tutorial to get this to work with Java 8, especially when you’re trying to build a packaged Java/Swing/Mac application. I’ll update this article as soon as I have time. For now, my tutorial on How to build a Mac/Java/Swing application on OS X 10.9 and Java 7 shows an Ant build script that needs to be used.

I'm not sure why, but your Java application name and menu system aren't placed on the Mac menubar by default; this is something you have to setup manually. Fortunately, it's pretty simple; just include two lines of code like this early on in your Java/Swing application:

System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ImageRotator");

In that example, the name of the Swing program I'm currently developing is ImageRotator, so that's the name that will show up on the Mac menubar.

As mentioned, I call these lines of code very early in my Java program. I don't remember exactly when they need to be called, but I assume that it's before you display any Swing components, so I typically get this out of the way right away, and call them in my application's main method.

A little more source code

Before I go, I thought I'd share the following lines of code straight out of my latest Java Swing app for the Mac OS X platform. Ignoring those pesky comments, the first two lines are exactly what I showed above, and the third line sets the application look and feel.

// take the menu bar off the jframe
System.setProperty("apple.laf.useScreenMenuBar", "true");

// set the name of the application menu item
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ImageRotator");

// set the look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

I thought I'd share those lines here just to provide a little more documentation.