Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native Swing Menu Bar Support For MacOS X In Java

Tags:

java

macos

swing

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?

like image 798
Kezzer Avatar asked Nov 20 '08 22:11

Kezzer


People also ask

Does Java Swing work on Mac?

The developer tools for Mac is freely available. If you want to (or have to) use Java and Swing you can use some libraries to create a GUI that looks well on Macs: Quaqua look and feel. MacWidgets.

What is Swing menu in Java?

JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems . JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).

How do you get the menu bar on a Mac?

Press Ctrl+F2 to move focus to the menu bar. This selects the Apple menu at the top-left of the display, whether or not you're in full-screen mode. To select a different menu, navigate using the left or right arrow keys; you'll see one of the menus highlighted, like the View menu below.


1 Answers

@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.

like image 98
Matt Solnit Avatar answered Sep 19 '22 07:09

Matt Solnit