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 {   Â// labels   ÂLabel l;   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating contextMenu ");       Â// create a label       ÂLabel label1 =newLabel("This is a ContextMenu example ");       Â// create a menu       ÂContextMenu contextMenu =newContextMenu();       Â// create menuitems       ÂMenuItem menuItem1 =newMenuItem("menu item 1");       ÂMenuItem menuItem2 =newMenuItem("menu item 2");       ÂMenuItem menuItem3 =newMenuItem("menu item 3");       Â// add menu items to menu       ÂcontextMenu.getItems().add(menuItem1);       ÂcontextMenu.getItems().add(menuItem2);       ÂcontextMenu.getItems().add(menuItem3);       Â// create a tilepane       ÂTilePane tilePane =newTilePane(label1);       Â// setContextMenu to label       Âlabel1.setContextMenu(contextMenu);       Â// create a scene       ÂScene sc =newScene(tilePane,200,200);       Â// set the scene       Âstage.setScene(sc);       Âstage.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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 {   Â// labels   ÂLabel label;   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating contextMenu ");       Â// create a label       ÂLabel label1 =newLabel("This is a ContextMenu example ");       Â// create a menu       ÂContextMenu contextMenu =newContextMenu();       Â// create menuitems       ÂMenuItem menuItem1 =newMenuItem("menu item 1");       ÂMenuItem menuItem2 =newMenuItem("menu item 2");       ÂMenuItem menuItem3 =newMenuItem("menu item 3");       Â// add menu items to menu       ÂcontextMenu.getItems().add(menuItem1);       ÂcontextMenu.getItems().add(menuItem2);       ÂcontextMenu.getItems().add(menuItem3);       Â// label to display events       ÂLabel label =newLabel("context menu hidden");       Â// create window event       ÂEventHandler<WindowEvent> event =newEventHandler<WindowEvent>() {           Âpublicvoidhandle(WindowEvent e)           Â{               Âif(contextMenu.isShowing())                   Âlabel.setText("context menu showing");               Âelse                   Âlabel.setText("context menu hidden");           Â}       Â};       Â// add event       ÂcontextMenu.setOnShowing(event);       ÂcontextMenu.setOnHiding(event);       Â// create a tilepane       ÂTilePane tilePane =newTilePane(label1);       ÂtilePane.getChildren().add(label);       Â// setContextMenu to label       Âlabel.setContextMenu(contextMenu);       Â// create a scene       ÂScene sc =newScene(tilePane,200,200);       Â// set the scene       Âstage.setScene(sc);       Âstage.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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




