I'm unable to make a JavaFX MenuBar show as a standard OS X menu bar, at the top of the screen.
Here's what I've tried in my subclass of Application:
public void start(Stage primaryStage) throws Exception { final Menu menu1 = new Menu("File"); final Menu menu2 = new Menu("Options"); final Menu menu3 = new Menu("Help"); MenuBar menuBar = new MenuBar(); menuBar.getMenus().addAll(menu1, menu2, menu3); menuBar.setUseSystemMenuBar(true); primaryStage.setTitle("Creating Menus with JavaFX 2.0"); final Group rootGroup = new Group(); final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT); rootGroup.getChildren().add(menuBar); primaryStage.setScene(scene); primaryStage.show(); }
I assumed that the use of
menuBar.setUseSystemMenuBar(true);
would do the trick, but actually it makes the menuBar disappear altogether.
I'm using Java 1.8.0-b132 on OS X 10.9
In Microsoft Windows, the menu bar is beneath the title bar. The menu bar in Windows may be accessed via keyboard shortcuts. Pressing the Alt and the menu-specific hotkey (which appears as an underlined letter in the menu) activates that menu choice.
JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.
I've had success with this code:
MenuBar menuBar = new MenuBar(); final String os = System.getProperty("os.name"); if (os != null && os.startsWith("Mac")) menuBar.useSystemMenuBarProperty().set(true); BorderPane borderPane = new BorderPane(); borderPane.setTop(menuBar); primaryStage.setScene(new Scene(borderPane));
It looks like OS X only displays the Menus if they have MenuItems inside them (which is a bit weird, as you can attach functionality to empty Menus).
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