JavaFX | MenuBar and Menu

Menu is a popup menu that contains several menu items that are displayed when the user clicks a menu. The user can select a menu item after which the menu goes into a hidden state.
MenuBar is usually placed at the top of the screen which contains several menus. JavaFX MenuBar is typically an implementation of a menu bar.
Constructor of the MenuBar class are:
- MenuBar(): creates a new empty menubar.
- MenuBar(Menu… m): creates a new menubar with the given set of menu.
Constructor of the Menu class are:
- Menu(): creates an empty menu
- Menu(String s): creates a menu with a string as its label
- Menu(String s, Node n):Constructs a Menu and sets the display text with the specified text and sets the graphic Node to the given node.
- Menu(String s, Node n, MenuItem… i):Constructs a Menu and sets the display text with the specified text, the graphic Node to the given node, and inserts the given items into the items list.
Commonly used methods:
| method | explanation |
|---|---|
| getItems() | returns the items of the menu |
| hide() | hide the menu |
| show() | show the menu |
| getMenus() | The menus to show within this MenuBar. |
| isUseSystemMenuBar() | Gets the value of the property useSystemMenuBar |
| setUseSystemMenuBar(boolean v) | Sets the value of the property useSystemMenuBar. |
| setOnHidden(EventHandler v) | Sets the value of the property onHidden. |
| setOnHiding(EventHandler v) | Sets the value of the property onHiding. |
| setOnShowing(EventHandler v) | Sets the value of the property onShowing. |
| setOnShown(EventHandler v | Sets the value of the property onShown. |
Below programs illustrate the MenuBar and Menu class:
- Java program to create a menu bar and add menu to it and also add menuitems to the menu: This program creates a menubar indicated by the name mb. A menu will be created by name m and 3 menuitems m1, m2, m3 will be added to the menu m and the menu m will be added to menubar mb. The menubar will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a VBox is created, on which addChildren() method is called to attach the menubar inside the scene. Finally, the show() method is called to display the final results.
// Java program to create a menu bar and add// menu to it and also add menuitems to menuimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclassMenuBar_1extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating MenuBar");// create a menuMenu m =newMenu("Menu");// create menuitemsMenuItem m1 =newMenuItem("menu item 1");MenuItem m2 =newMenuItem("menu item 2");MenuItem m3 =newMenuItem("menu item 3");// add menu items to menum.getItems().add(m1);m.getItems().add(m2);m.getItems().add(m3);// create a menubarMenuBar mb =newMenuBar();// add menu to menubarmb.getMenus().add(m);// create a VBoxVBox vb =newVBox(mb);// create a sceneScene sc =newScene(vb,500,300);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to create a menu bar and add a menu to it and also add menu items to menu and also add an event listener to handle the events: This program creates a menubar indicated by the name mb. A menu will be created by name m and 3 menuitems m1, m2, m3 will be added to the menu m and the menu m will be added to the menubar mb. The menubar will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a VBox is created, on which addChildren() method is called to attach the menubar inside the scene. Finally, the show() method is called to display the final results. A label will also be created that will show which menuitem is selected. An action event will be created to process the action when the menu item is clicked by the user.
// Java program to create a menu bar and add menu to// it and also add menuitems to menu and also add// an event listener to handle the eventsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;importjava.time.LocalDate;publicclassMenuBar_2extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating MenuBar");// create a menuMenu m =newMenu("Menu");// create menuitemsMenuItem m1 =newMenuItem("menu item 1");MenuItem m2 =newMenuItem("menu item 2");MenuItem m3 =newMenuItem("menu item 3");// add menu items to menum.getItems().add(m1);m.getItems().add(m2);m.getItems().add(m3);// label to display eventsLabel l =newLabel("\t\t\t\t"+"no menu item selected");// create events for menu items// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l.setText("\t\t\t\t"+ ((MenuItem)e.getSource()).getText() +" selected");}};// add eventm1.setOnAction(event);m2.setOnAction(event);m3.setOnAction(event);// create a menubarMenuBar mb =newMenuBar();// add menu to menubarmb.getMenus().add(m);// create a VBoxVBox vb =newVBox(mb, l);// create a sceneScene sc =newScene(vb,500,300);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE please use an offline converter.
Reference:



