Open In App

JavaFX | ContextMenu with examples

Improve
Improve
Like Article
Like
Save
Share
Report


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:

  1. ContextMenu(): creates a new empty context menu.
  2. 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:

  1. 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 label
    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 contextMenu_1 extends Application {
        // labels
        Label l;
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating contextMenu ");
      
            // create a label
            Label label1 = new Label("This is a ContextMenu example ");
      
            // create a menu
            ContextMenu contextMenu = new ContextMenu();
      
            // create menuitems
            MenuItem menuItem1 = new MenuItem("menu item 1");
            MenuItem menuItem2 = new MenuItem("menu item 2");
            MenuItem menuItem3 = new MenuItem("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 = new TilePane(label1);
      
            // setContextMenu to label
            label1.setContextMenu(contextMenu);
      
            // create a scene
            Scene sc = new Scene(tilePane, 200, 200);
      
            // set the scene
            stage.setScene(sc);
      
            stage.show();
        }
      
        public static void main(String args[])
        {
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  2. 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 listener
    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.collections.*;
    import javafx.stage.Stage;
    import javafx.scene.text.Text.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    public class contextMenu extends Application {
        // labels
        Label label;
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating contextMenu ");
      
            // create a label
            Label label1 = new Label("This is a ContextMenu example ");
      
            // create a menu
            ContextMenu contextMenu = new ContextMenu();
      
            // create menuitems
            MenuItem menuItem1 = new MenuItem("menu item 1");
            MenuItem menuItem2 = new MenuItem("menu item 2");
            MenuItem menuItem3 = new MenuItem("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 = new Label("context menu hidden");
      
            // create window event
            EventHandler<WindowEvent> event = new EventHandler<WindowEvent>() {
                public void handle(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 = new TilePane(label1);
      
            tilePane.getChildren().add(label);
      
            // setContextMenu to label
            label.setContextMenu(contextMenu);
      
            // create a scene
            Scene sc = new Scene(tilePane, 200, 200);
      
            // set the scene
            stage.setScene(sc);
      
            stage.show();
        }
      
        public static void main(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



Last Updated : 24 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads