Skip to content
Related Articles

Related Articles

Improve Article

JavaFX | MenuButton

  • Last Updated : 23 Oct, 2019

MenuButton is a part of the JavaFX library. The menuButton when pressed shows a context menu that displays a set of items and the user may select any item. It typically contains several menu items and the user may select at most one MenuItem at a time.

Constructor of the MenuButton class are:

  1. MenuButton(): creates a new menu button
  2. MenuButton(String t): creates a menubutton with a specified text
  3. MenuButton(String t, Node g): creates a menubutton with a specified text
    and graphic
  4. MenuButton(String t, Node g, MenuItem… i) creates a menubutton with a specified text, graphic and menuitems

Commonly used methods:

methodexplanation
getItems()returns the items of the menu button
getPopupSide()get the velue of property popupSide
hide()hides the context menu
isShowing()Gets the value of the property showing.
setPopupSide(Side v)Sets the value of the property popupSide.
show()shows the context menu

Below programs illustrate the MenuButton class:

  1. Program to create a MenuButton and add MenuItems to it: A MenuButton will be created by name m and 3 menuitems m1, m2, m3 will be added to the menuButton m. 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 tilepane is created, on which addChildren() method is called to attach the menubutton inside the scene. Finally, the show() method is called to display the final results.




    // Program to create a menubutton and add menuitems to it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.collections.*;
    import javafx.stage.Stage;
    import javafx.scene.text.Text.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    public class MenuButton_1 extends Application {
        // labels
        Label l;
      
        // launch the application
        public void start(Stage s)
        {
            // set title for the stage
            s.setTitle("creating MenuButton ");
      
            // create a tile pane
            TilePane r = new TilePane();
      
            // create a label
            Label l1 = new Label("This is a MenuButton example ");
      
            // create a menu button
            MenuButton m = new MenuButton("menuButton");
      
            // 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 tilepane
            TilePane vb = new TilePane(l1, m);
      
            // create a scene
            Scene sc = new Scene(vb, 200, 200);
      
            // set the scene
            s.setScene(sc);
      
            s.show();
        }
      
        public static void main(String args[])
        {
            // launch the application
            launch(args);
        }
    }

    Output:

  2. Program to create a menubutton and add menuitems to it and also add an event handler to handle events: A menuButton will be created by name m and 3 menuitems m1, m2, m3 will be added to the menuButton m. 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 tilepane is created, on which addChildren() method is called to attach the menubutton inside the scene. Finally, the show() method is called to display the final results. Event handler will be created that will handle the events of the menu items. A label l2 will be created to show which menuitem is selected.




    // Program to create a menubutton and add menuitems
    // to it and also add event handler to handle events
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.WindowEvent;
    import javafx.event.EventHandler.*;
    import javafx.event.EventHandler;
    import javafx.event.ActionEvent;
    import javafx.collections.*;
    import javafx.stage.Stage;
    import javafx.scene.text.Text.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    public class MenuButton_2 extends Application {
        // labels
        Label l;
      
        // launch the application
        public void start(Stage s)
        {
            // set title for the stage
            s.setTitle("creating MenuButton ");
      
            // create a tile pane
            TilePane r = new TilePane();
      
            // create a label
            Label l1 = new Label("This is a MenuButton example ");
      
            // create a menu
            MenuButton m = new MenuButton("MenuButton");
      
            // 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 the selected menuitem
            Label l2 = new Label("default menuitem selected");
      
            // create action event
            EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {
                public void handle(ActionEvent e)
                {
                    l2.setText(((MenuItem)e.getSource()).getText() + " selected");
                }
            };
      
            // add action events to the menuitems
            m1.setOnAction(event1);
            m3.setOnAction(event1);
            m2.setOnAction(event1);
      
            // create a tilepane
            TilePane vb = new TilePane(l1);
      
            vb.getChildren().add(m);
            vb.getChildren().add(l2);
      
            // create a scene
            Scene sc = new Scene(vb, 200, 200);
      
            // 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 compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/MenuButton.html

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :