Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an OS X menubar in JavaFX

Tags:

javafx-2

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

like image 929
Steve McLeod Avatar asked Mar 21 '14 20:03

Steve McLeod


People also ask

How do I make a menubar?

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.

What is JavaFX used for?

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.


2 Answers

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)); 
like image 99
dmolony Avatar answered Sep 18 '22 16:09

dmolony


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

like image 37
James_D Avatar answered Sep 19 '22 16:09

James_D