OSX System menubar not working in JavaFX

2019-09-07 17:59发布

问题:

I was trying to use NSMenuFX from https://github.com/codecentric/NSMenuFX to make a JavaFX app use the MacOS System MenuBar, and it didn't work because of this method returning always false.

Toolkit.getToolkit().getSystemMenu().isSupported()

The method is from the package : com.sun.javafx.tk.Toolkit.

Going deeper in the code I've found that Toolkit.getToolkit().getSystemMenu().isSupported() calls a method from com.sun.glass.ui.Application that returns always false too.

protected boolean _supportsSystemMenu() {
    return false;
}

public final boolean supportsSystemMenu() {
    checkEventThread();
    return this._supportsSystemMenu();
}

Is there something wrong about this code, and how can i get JavaFX app using the System menubar.

NB :Used JDK is 8u121 on OSX 10.12.3.

Edit 1 : As suggested in comments, here is some code.

import javafx.scene.control.MenuBar;

public class MyappMenuBar extends MenuBar {
// member variables -------------------------------------------------------------------------
    private final MyappPane mmyappPane;
    public MyappPane getMyappPane() {return mMyappPane;}
    private final MyappHelpMenu mHelpMenu;
    public MyappHelpMenu getHelpMenu() {return mHelpMenu;}

// constructors -----------------------------------------------------------------------------
    public myappMenuBar(MyappPane pMyappPane) {
        mMyappPane = pMyappPane;
        setUseSystemMenuBar(true);
        mHelpMenu = new MyappHelpMenu(pMyappPane);
        getMenus().add(mHelpMenu);
    }
}

回答1:

The problem was caused by the splash screen (java -splash:../img.png), as it is an AWT feature and not a JavaFX feature , nothing seems to help combine the AWT and JavaFX threads together in a single thread and -Djavafx.embed.singleThread=true doesn't seem to work.

For now I just disabled the splash screen and I plan to implement it as a JavaFX Pane from within the app.