JavaFX | MenuButton
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:
- MenuButton(): creates a new menu button
- MenuButton(String t): creates a menubutton with a specified text
- MenuButton(String t, Node g): creates a menubutton with a specified text
and graphic - MenuButton(String t, Node g, MenuItem… i) creates a menubutton with a specified text, graphic and menuitems
Commonly used methods:
| method | explanation |
|---|---|
| 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:
- 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 itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;publicclassMenuButton_1extendsApplication {// labelsLabel l;// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating MenuButton ");// create a tile paneTilePane r =newTilePane();// create a labelLabel l1 =newLabel("This is a MenuButton example ");// create a menu buttonMenuButton m =newMenuButton("menuButton");// 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 tilepaneTilePane vb =newTilePane(l1, m);// create a sceneScene sc =newScene(vb,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:

- 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 eventsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.WindowEvent;importjavafx.event.EventHandler.*;importjavafx.event.EventHandler;importjavafx.event.ActionEvent;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;publicclassMenuButton_2extendsApplication {// labelsLabel l;// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating MenuButton ");// create a tile paneTilePane r =newTilePane();// create a labelLabel l1 =newLabel("This is a MenuButton example ");// create a menuMenuButton m =newMenuButton("MenuButton");// 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 the selected menuitemLabel l2 =newLabel("default menuitem selected");// create action eventEventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){l2.setText(((MenuItem)e.getSource()).getText() +" selected");}};// add action events to the menuitemsm1.setOnAction(event1);m3.setOnAction(event1);m2.setOnAction(event1);// create a tilepaneTilePane vb =newTilePane(l1);vb.getChildren().add(m);vb.getChildren().add(l2);// create a sceneScene sc =newScene(vb,200,200);// 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 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.


