JavaFX | MenuBar and Menu - GeeksforGeeks

25 Oct, 2019
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.
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 menuMenu(String s): creates a menu with a string as its labelMenu(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.
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:
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 menuimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;import java.time.LocalDate;public class MenuBar_1 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating MenuBar");          // create a menu        Menu m = new Menu("Menu");          // create menuitems        MenuItem m1 = new MenuItem("menu item 1");        MenuItem m2 = new MenuItem("menu item 2");        MenuItem m3 = new MenuItem("menu item 3");          // add menu items to menu        m.getItems().add(m1);        m.getItems().add(m2);        m.getItems().add(m3);          // create a menubar        MenuBar mb = new MenuBar();          // add menu to menubar        mb.getMenus().add(m);          // create a VBox        VBox vb = new VBox(mb);          // create a scene        Scene sc = new Scene(vb, 500, 300);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(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 eventsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;import java.time.LocalDate;public class MenuBar_2 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating MenuBar");          // create a menu        Menu m = new Menu("Menu");          // create menuitems        MenuItem m1 = new MenuItem("menu item 1");        MenuItem m2 = new MenuItem("menu item 2");        MenuItem m3 = new MenuItem("menu item 3");          // add menu items to menu        m.getItems().add(m1);        m.getItems().add(m2);        m.getItems().add(m3);          // label to display events        Label l = new Label("\t\t\t\t"                            + "no menu item selected");          // create events for menu items        // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("\t\t\t\t" + ((MenuItem)e.getSource()).getText() +                                                                " selected");            }        };          // add event        m1.setOnAction(event);        m2.setOnAction(event);        m3.setOnAction(event);          // create a menubar        MenuBar mb = new MenuBar();          // add menu to menubar        mb.getMenus().add(m);          // create a VBox        VBox vb = new VBox(mb, l);          // create a scene        Scene sc = new Scene(vb, 500, 300);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Note: The above programs might not run in an online IDE please use an offline converter.Reference:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Menu.htmlhttps://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/MenuBar.htmlMy Personal Notes
arrow_drop_upSave
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 menuimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;import java.time.LocalDate;public class MenuBar_1 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating MenuBar");          // create a menu        Menu m = new Menu("Menu");          // create menuitems        MenuItem m1 = new MenuItem("menu item 1");        MenuItem m2 = new MenuItem("menu item 2");        MenuItem m3 = new MenuItem("menu item 3");          // add menu items to menu        m.getItems().add(m1);        m.getItems().add(m2);        m.getItems().add(m3);          // create a menubar        MenuBar mb = new MenuBar();          // add menu to menubar        mb.getMenus().add(m);          // create a VBox        VBox vb = new VBox(mb);          // create a scene        Scene sc = new Scene(vb, 500, 300);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
// Java program to create a menu bar and add// menu to it and also add menuitems to menuimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;import java.time.LocalDate;public class MenuBar_1 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating MenuBar");          // create a menu        Menu m = new Menu("Menu");          // create menuitems        MenuItem m1 = new MenuItem("menu item 1");        MenuItem m2 = new MenuItem("menu item 2");        MenuItem m3 = new MenuItem("menu item 3");          // add menu items to menu        m.getItems().add(m1);        m.getItems().add(m2);        m.getItems().add(m3);          // create a menubar        MenuBar mb = new MenuBar();          // add menu to menubar        mb.getMenus().add(m);          // create a VBox        VBox vb = new VBox(mb);          // create a scene        Scene sc = new Scene(vb, 500, 300);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(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 eventsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;import java.time.LocalDate;public class MenuBar_2 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating MenuBar");          // create a menu        Menu m = new Menu("Menu");          // create menuitems        MenuItem m1 = new MenuItem("menu item 1");        MenuItem m2 = new MenuItem("menu item 2");        MenuItem m3 = new MenuItem("menu item 3");          // add menu items to menu        m.getItems().add(m1);        m.getItems().add(m2);        m.getItems().add(m3);          // label to display events        Label l = new Label("\t\t\t\t"                            + "no menu item selected");          // create events for menu items        // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("\t\t\t\t" + ((MenuItem)e.getSource()).getText() +                                                                " selected");            }        };          // add event        m1.setOnAction(event);        m2.setOnAction(event);        m3.setOnAction(event);          // create a menubar        MenuBar mb = new MenuBar();          // add menu to menubar        mb.getMenus().add(m);          // create a VBox        VBox vb = new VBox(mb, l);          // create a scene        Scene sc = new Scene(vb, 500, 300);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Note: The above programs might not run in an online IDE please use an offline converter.Reference:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Menu.htmlhttps://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/MenuBar.htmlMy Personal Notes
arrow_drop_upSave
// 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 eventsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;import java.time.LocalDate;public class MenuBar_2 extends Application {      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating MenuBar");          // create a menu        Menu m = new Menu("Menu");          // create menuitems        MenuItem m1 = new MenuItem("menu item 1");        MenuItem m2 = new MenuItem("menu item 2");        MenuItem m3 = new MenuItem("menu item 3");          // add menu items to menu        m.getItems().add(m1);        m.getItems().add(m2);        m.getItems().add(m3);          // label to display events        Label l = new Label("\t\t\t\t"                            + "no menu item selected");          // create events for menu items        // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                l.setText("\t\t\t\t" + ((MenuItem)e.getSource()).getText() +                                                                " selected");            }        };          // add event        m1.setOnAction(event);        m2.setOnAction(event);        m3.setOnAction(event);          // create a menubar        MenuBar mb = new MenuBar();          // add menu to menubar        mb.getMenus().add(m);          // create a VBox        VBox vb = new VBox(mb, l);          // create a scene        Scene sc = new Scene(vb, 500, 300);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE please use an offline converter.
Reference:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Menu.html
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/MenuBar.html
ManasChhabra2
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
LinkedList in Java
Stack Class in Java
Overriding in Java
Set in Java
Collections in Java
Multithreading in Java