JavaFX | CustomMenuItem
CustomMenuItem is a part of the JavaFX library. CustomMenuItem allows different types of nodes as its menu item. One of the useful property of the customMenuItem is hideOnClick. This property states whether the menu should be hidden when the user clicks the menuitem or not.
Constructors of CustomMenuItem:
- CustomMenuItem(Node n): creates a menuitem with the node specified
- CustomMenuItem(Node n, boolean b): creates a menuitem with the node and hide on click property specified
Commonly used methods:
| method | explanantion |
|---|---|
| getContent() | gets the value of the property content |
| isHideOnClick() | gets the value of the property hideOnClick |
| setHideOnClick(boolean v) | sets the value of the property hideOnClick |
| setContent(Node v) | sets the node as the content for the menu item |
Below programs will illustrate the use of CustomMenuItem:
- Program to Create a custom menu items and add it to the menu: This program creates a menubar indicated by the name menubar. A menu will be created by name m and 3 custom menuitems menuitem_1, menuitem_2, menuitem_3 will be added to the menu and the menu will be added to the menubar. The menubar will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide a 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. The CustonMenuItems will contain a button, label, and a checkbox.
// Program to create a custom menu items and add it to the menuimportjavafx.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.*;publicclassCustomMenuItem_1extendsApplication {// Launch the applicationpublicvoidstart(Stage stage){// Set title for the stagestage.setTitle("creating CustomMenuItem ");// Create a tile paneTilePane r =newTilePane();// Create a labelLabel description_label =newLabel("This is a CustomMenuItem example ");// Create a menuMenu menu =newMenu("Menu");// Create menuitemsCustomMenuItem menuitem_1 =newCustomMenuItem(newButton("MenuItem 1"));CustomMenuItem menuitem_2 =newCustomMenuItem(newLabel("MenuItem 2"));CustomMenuItem menuitem_3 =newCustomMenuItem(newCheckBox("MenuItem 3"));// Add menu items to menumenu.getItems().add(menuitem_1);menu.getItems().add(menuitem_2);menu.getItems().add(menuitem_3);// Create a menubarMenuBar menubar =newMenuBar();// Add menu to menubarmenubar.getMenus().add(menu);// Create a VBoxVBox vbox =newVBox(menubar);// Create a sceneScene scene =newScene(vbox,200,200);// Set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// Launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- Program to create custom menu items and add it to the menu and use the property hide on click: This program creates a menubar indicated by the name menubar. A menu will be created by name m and 4 custom menuitems menuitem_1, menuitem_2, menuitem_3, menuitem_4 will be added to the menu and the menu will be added to the menubar. The menubar will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide a 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. The CustonMenuItems will contain a button, a slider, a checkbox, and a choicebox. The hide on click property of custom menuitems_2 and menuitems_4 will be set to false and of menuitem_1 and menuitems_3 will be set to true. On Clicking on mneuitem_2 and menuitem_4 will not disappear on clicking.
// Program to create custom menu items and// Add it to the menu and use the property hide on clickimportjavafx.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.*;publicclassCustomMenuItem_2extendsApplication {// Launch the applicationpublicvoidstart(Stage stage){// Set title for the stagestage.setTitle("creating CustomMenuItem ");// Create a tile paneTilePane r =newTilePane();// Create a labelLabel description_label =newLabel("This is a CustomMenuItem example ");// Create a menuMenu menu =newMenu("Menu");// Create menuitemsCustomMenuItem menuitem_1 =newCustomMenuItem(newButton("MenuItem 1"));CustomMenuItem menuitem_2 =newCustomMenuItem(newSlider());CustomMenuItem menuitem_3 =newCustomMenuItem(newCheckBox("MenuItem 3"));CustomMenuItem menuitem_4 =newCustomMenuItem(newChoiceBox(FXCollections.observableArrayList("choice 1","choice 2","choice 3")));// Cet hide on click propertymenuitem_2.setHideOnClick(false);menuitem_4.setHideOnClick(false);menuitem_1.setHideOnClick(true);menuitem_3.setHideOnClick(true);// Add menu items to menumenu.getItems().add(menuitem_1);menu.getItems().add(menuitem_2);menu.getItems().add(menuitem_3);menu.getItems().add(menuitem_4);// Create a menubarMenuBar menubar =newMenuBar();// Add menu to menubarmenubar.getMenus().add(menu);// Create a VBoxVBox vbox =newVBox(menubar);// Create a sceneScene scene =newScene(vbox,200,200);// Cet the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// Launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/control/CustomMenuItem.html
Recommended Posts:
- JavaFX | ChoiceBox
- JavaFX | Tooltip
- JavaFX | Checkbox
- JavaFX | TextInputDialog
- JavaFX | ChoiceDialog
- JavaFX | MenuButton
- JavaFX | Pos Class
- JavaFX | Arc with examples
- JavaFX | Box with examples
- JavaFX | Tab Class
- JavaFX | ProgressIndicator
- JavaFX | ProgressBar
- JavaFX | Label
- JavaFx | PasswordField
- JavaFX | TextField
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



