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.

Comments

If you can provide some more information, I'll be glad to try to help. This has worked for me on quite a few applications now, including Mac Java applications I use every day.

Also, for more detailed coverage than I show in this article, I have this longer tutorial named "How Make Java Applications Look and Feel Like Native mac Applications."

Permalink

I think it's worth noting that it works fine provided you call setProperty before setting the look and feel (as in the example code).

Just put it in the first line of you main class. then it works.

If you can provide some more information, I'll see if I can help. In the meantime, here's some code I just pulled from one of my Mac Java apps. Try calling this method immediately in your main() method:

private void configureForMacOSX()
{
  // set some mac-specific properties; helps when i don't use ant to build the code
  System.setProperty("apple.awt.graphics.EnableQ2DX", "true");
  System.setProperty("apple.laf.useScreenMenuBar", "true");
  System.setProperty("com.apple.mrj.application.apple.menu.about.name", APP_NAME);

  // create an instance of the Mac Application class, so i can handle the 
  // mac quit event with the Mac ApplicationAdapter
  Application macApplication = Application.getApplication();
  MyApplicationAdapter macAdapter = new MyApplicationAdapter(this);
  macApplication.addApplicationListener(macAdapter);

  // must enable the preferences option manually
  macApplication.setEnabledPreferencesMenu(true);
}

You may not need all the Application and adapter code, but I thought I'd include it anyway.

Also, I assume this only works with the Apple-supplied JVM. If you're using something like OpenJDK this won't work, at least not in early 2011.