Open In App

JavaFX | MenuBar and Menu

Last Updated : 25 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. MenuBar(): creates a new empty menubar.
  2. MenuBar(Menu… m): creates a new menubar with the given set of menu.

Constructor of the Menu class are:

  1. Menu(): creates an empty menu
  2. Menu(String s): creates a menu with a string as its label
  3. 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.
  4. 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:

  1. 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 menu
    import 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:

  2. 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 events
    import 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:

  3. Note: The above programs might not run in an online IDE please use an offline converter.

    Reference:



    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

Similar Reads