JavaFX | ContextMenu with examples

ContextMenu is a part of the JavaFX library. ContextMenu can be associated with controls such as labels, textfield etc. The context menu is activated on right clicking over the associated controls. It shows a popup containing several menuitems or sub menu.
The Constructors of the class are:
- ContextMenu(): creates a new empty context menu.
- ContextMenu(MenuItem… i): creates a context menu that contains the menuitems.
Commonly used methods:
| method | explanation |
|---|---|
| getItems() | returns the items of the context menu |
| getOnAction() | returns the value of the property OnAction |
| hide() | hides the context menu |
| setOnAction(EventHandler v) | sets the value of the property onAction |
| show(Node a, double X, double Y) | displays the context menu at a specified position of the screen |
Below programs illustrate the use of ContextMenu:
- Program to create a context menu and add it to label: A ContextMenu will be created by name ‘contextMenu’ and 3 menuitems: menuItem1, menuItem2, menuItem3 will be added to the menu contextMenu and the menu contextMenu will be associated with a label ‘label’. The label 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.
// Program to create a context menu and add it to labelimportjavafx.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.*;publicclasscontextMenu_1extendsApplication {// labelsLabel l;// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating contextMenu ");// create a labelLabel label1 =newLabel("This is a ContextMenu example ");// create a menuContextMenu contextMenu =newContextMenu();// create menuitemsMenuItem menuItem1 =newMenuItem("menu item 1");MenuItem menuItem2 =newMenuItem("menu item 2");MenuItem menuItem3 =newMenuItem("menu item 3");// add menu items to menucontextMenu.getItems().add(menuItem1);contextMenu.getItems().add(menuItem2);contextMenu.getItems().add(menuItem3);// create a tilepaneTilePane tilePane =newTilePane(label1);// setContextMenu to labellabel1.setContextMenu(contextMenu);// create a sceneScene sc =newScene(tilePane,200,200);// set the scenestage.setScene(sc);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Program to create a context menu and add it to label and associate the context menu with window event listener: A Contextmenu will be created by name contextMenu and 3 menuitems menuItem1, menuItem2, menuItem3 will be added to the menu contextMenu and the contextMenu will be associated with a label l. The label 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 Window event will be created that will handle the window events of the context menu and will display the state of the context menu by a Label ‘label’. The window event will be associated with the label using setOnHiding() and setOnShowing() functions.
// Program to create a context menu and add it to label// and associate the context menu with window event listenerimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.WindowEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;importjavafx.scene.text.Text.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;publicclasscontextMenuextendsApplication {// labelsLabel label;// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating contextMenu ");// create a labelLabel label1 =newLabel("This is a ContextMenu example ");// create a menuContextMenu contextMenu =newContextMenu();// create menuitemsMenuItem menuItem1 =newMenuItem("menu item 1");MenuItem menuItem2 =newMenuItem("menu item 2");MenuItem menuItem3 =newMenuItem("menu item 3");// add menu items to menucontextMenu.getItems().add(menuItem1);contextMenu.getItems().add(menuItem2);contextMenu.getItems().add(menuItem3);// label to display eventsLabel label =newLabel("context menu hidden");// create window eventEventHandler<WindowEvent> event =newEventHandler<WindowEvent>() {publicvoidhandle(WindowEvent e){if(contextMenu.isShowing())label.setText("context menu showing");elselabel.setText("context menu hidden");}};// add eventcontextMenu.setOnShowing(event);contextMenu.setOnHiding(event);// create a tilepaneTilePane tilePane =newTilePane(label1);tilePane.getChildren().add(label);// setContextMenu to labellabel.setContextMenu(contextMenu);// create a sceneScene sc =newScene(tilePane,200,200);// set the scenestage.setScene(sc);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note : The above programs might not run in an online compiler please use an offline IDE.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html




