I want to add a JavaFX Menu Bar to a Stage, but have it use the System Menubar for Mac.
My problem is that using:
menuBar.useSystemMenuBarProperty().set(true);
does not work. I believe that the problem is because my main method isn't part of a JavaFX Application. My main method looks like this:
public class SURPG_Launcher {
public static com.apple.eawt.Application macApp;
public static void main(String[] args) {
if(Toolbox.isMac()) {
initMac(args);
} else {
Application.launch(SURPG_Main.class, args);
}
}
private static void initMac(String[] args) {
System.out.println("MacOS System detected!");
macApp = com.apple.eawt.Application.getApplication();
macApp.setPreferencesHandler(new PreferencesHandler(){
@Override
public void handlePreferences(PreferencesEvent arg0) {
Platform.runLater(() -> {
Stage prefs = new Stage();
prefs.setMinHeight(200);
prefs.setMinWidth(200);
prefs.show();
});
}
});
Application.launch(SURPG_Mac.class, args);
}
}
SURPG_Mac.class and SURPG_Main.class are classes that extend the JavaFX Application.
I have another class that sets the GUI, a stage with a BorderPane. I have another class with public static methods that can be called to set the Menubars, as such:
public class MenuControl {
public static MenuBar menuBar;
public static Menu menuFile;
public static Menu menuGame;
public static Menu menuTools;
public static MenuItem save;
public static void initMenusMac() {
menuBar = new MenuBar();
Platform.runLater(() -> {
menuBar.useSystemMenuBarProperty().set(true);
});
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
public static void initMenus() {
menuBar = new MenuBar();
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
}
My final point is that I CANNOT change it so the main method is in either SURPG_Mac or SURPG_Main, due to a different compatibility problem with Mac integration.
Can anyone help me with this?
Thank you so much in advance!
Have a look at this probject: https://github.com/codecentric/NSMenuFX It allows you to have a more mac-like menu bar. But you will probably have to clean up your somewhat strange project setup as well before you can use it.