17

A link that stands out is http://www.devdaily.com/blog/post/jfc-swing/handling-main-mac-menu-in-swing-application/ however the menu bar under Mac OS X displays as the package name as opposed to the application name. I'm using the code in the above link without any luck, so I'm unsure if anything's changed in recent Mac OS versions.

Here's an extract:

public RootGUI() {
    super("Hello");
    JMenuBar menuBar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenuItem item = new JMenuItem("Woah");
    file.add(item);
    menuBar.add(file);
    setJMenuBar(menuBar);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(100, 100);
    pack();
    setVisible(true);
}
public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
                System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                new RootGUI();
            }
            catch(ClassNotFoundException e) {
                System.out.println("ClassNotFoundException: " + e.getMessage());
            }
            catch(InstantiationException e) {
                System.out.println("InstantiationException: " + e.getMessage());
            }
            catch(IllegalAccessException e) {
                System.out.println("IllegalAccessException: " + e.getMessage());
            }
            catch(UnsupportedLookAndFeelException e) {
                System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
            }

        }
    });
}

The first menu item on the menu bar should display as "test", unfortunately this isn't the case. The file menu works fine, on the other hand. Any ideas?

Kieran Senior
  • 17,960
  • 26
  • 94
  • 138

7 Answers7

23

@Kezzer

I think I see what's going on. If you put the main() method in a different class, then everything works. So you need something like:

public class RootGUILauncher {
  public static void main(String[] args) {
    try {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
                System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(ClassNotFoundException e) {
                System.out.println("ClassNotFoundException: " + e.getMessage());
        }
        catch(InstantiationException e) {
                System.out.println("InstantiationException: " + e.getMessage());
        }
        catch(IllegalAccessException e) {
                System.out.println("IllegalAccessException: " + e.getMessage());
        }
        catch(UnsupportedLookAndFeelException e) {
                System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new RootGUI();
        }
    });
}

And then put your RootGUI class in a different file.

Matt Solnit
  • 32,152
  • 8
  • 53
  • 57
10

You need to set the "com.apple.mrj.application.apple.menu.about.name" system property in the main thread, not in the Swing thread (in other words, just make it the first line in the program).

Matt Solnit
  • 32,152
  • 8
  • 53
  • 57
3

As I understand you want to rename your application menu shown on the os x menu bar. Well, I didn't find a system property but I found a command line option:

-Xdock:name="YourNameHere"

that worked for me.

BTW: The syystem property com.apple.mrj.application.apple.menu.about.name is for renaming the about menu item in your application menu, not the menu bar itself

See this link here (the old link was probably killed sometime after the sun-oracle-aquisition).

Daniel Hiller
  • 3,415
  • 3
  • 23
  • 33
0

If you are launching multiple JFrames and you use the mac menu, then one of the JFrames could be replacing the JFrame you want with one that has a different menu structure. I'm not sure if you can share one JMenuBar for multiple JFrames, but you could just make an application wide menubar that all the frames instantiate.

ramsey0
  • 1,587
  • 1
  • 12
  • 10
0

If you want to deliver an application that looks native on Mac OS X, one important part is to deliver an appplication bundle. Within the application bundle, you will be able to provide a property list file in order to solve this problems.

Some official info: Java Development Guide for Mac OS X

Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
0

for anybody interested, although this question is 6 years old i had the same problem. Swing menus aren't displayed in the mac native bar. I found an easier and more straightforward solution... Just add to your JFrame a Java.awt Menu Component instead of a JMenu and it will automatically display in the native Menubar!

0

Talk about a late hit, but I can confirm that (amazingly)

System.setProperty("apple.laf.useScreenMenuBar", "true");

...and then setting a JMenuBar on the JFrame worked, putting the menu bar on the top as preferred by macOS.

jFrame.setJMenuBar(jMenuBar);

...running on macOS Big Sur (11.2.3) and JDK 16.

Will Iverson
  • 2,009
  • 12
  • 22