Native Swing Menu Bar Support For MacOS X In Java

后端 未结 7 1961
北海茫月
北海茫月 2020-12-01 09:54

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 packag

相关标签:
7条回答
  • 2020-12-01 10:14

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

    0 讨论(0)
  • 2020-12-01 10:21

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

    0 讨论(0)
  • 2020-12-01 10:25

    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 讨论(0)
  • 2020-12-01 10:26

    You can also use Macify when you build the app so you don't need to change any code.

    0 讨论(0)
  • 2020-12-01 10:29

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

    0 讨论(0)
  • 2020-12-01 10:29

    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

    0 讨论(0)
提交回复
热议问题