So I've looked around a fair bit, but I've not been able to find any information on how to make an application menu in JavaFX.
I've seen a project 'Jayatana' which seems to allow applications to have proper application menus in Ubuntu using Intellij at least (as an example).
I've also seen a few suggestions that using something like the following will work for OS X users:-
final List<MenuBase> menus = new ArrayList<>();
menus.add(GlobalMenuAdapter.adapt(menu));
Toolkit.getToolkit().getSystemMenu().setMenus(menus);
And there is also the NSMenuFX project, again for OS X.
And I've also seen the java-gnome project which I think only works for Swing.
But what I'd really like is some way of making application menus, preferably in a non-OS specific manner.
I'm happy to use a third party jar or whatever which does the heavy lifting but really, does anything like this exist?
At this point would my best bet be using Swing to create the shell of the JavaFX application and use methods which will integrate application menus with Swing instead? If that's the case, is there something that can do this automatically from JavaFX and handle the switching of the differing implementations?
edit
In the end, I simply used a combination of both Swing and JavaFX. I put the JavaFX app inside which allowed me to use the application menus which already work in Swing.
Not ideal, but it did work.
A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content. Example 1-1 creates the stage and scene and makes the scene visible in a given pixel size.
I think you are just looking for MenuBar.useSystemMenuBarProperty()
. If you call this method on a menu bar, then if the platform supports system menus (e.g. OS X) the menu will not appear in the scene graph but will be used as the system menu.
SSCCE:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class SystemMenuExample extends Application {
@Override
public void start(Stage primaryStage) {
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("File");
MenuItem quit = new MenuItem("Quit");
quit.setOnAction(e -> Platform.exit());
menu.getItems().add(quit);
menuBar.getMenus().add(menu);
menuBar.setUseSystemMenuBar(true);
BorderPane root = new BorderPane();
root.setTop(menuBar);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With