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("", "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("", "ImageRotator");

// set the look and feel

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



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("", "true");
  System.setProperty("apple.laf.useScreenMenuBar", "true");
  System.setProperty("", 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);

  // must enable the preferences option manually

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.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.