JavaFX ContexMenu

Jakob Jenkov
Last update: 2019-07-21

The JavaFX ContextMenu component provides a standard right click menu for JavaFX controls. The JavaFX ContextMenu is represented by the class javafx.scene.control.ContextMenu. You create a ContextMenu instance and attach it to the JavaFX control you want the ContextMenu to be active for. Here is a screenshot of a JavaFX TextArea with a ContextMenu attached and shown:

JavaFX ContextMenu attached to a TextArea.

Full ContextMenu Example

Here is a full JavaFX ContextMenu example to give you a quick overview of how using the ContextMenu looks:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ContextMenuExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        ContextMenu contextMenu = new ContextMenu();
        MenuItem menuItem1 = new MenuItem("Choice 1");
        MenuItem menuItem2 = new MenuItem("Choice 2");
        MenuItem menuItem3 = new MenuItem("Choice 3");

        menuItem3.setOnAction((event) -> {
            System.out.println("Choice 3 clicked!");
        });

        contextMenu.getItems().addAll(menuItem1,menuItem2,menuItem3);

        TextArea textArea = new TextArea();

        textArea.setContextMenu(contextMenu);

        VBox vBox = new VBox(textArea);
        Scene scene = new Scene(vBox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX App");

        primaryStage.show();
    }
}

Create a ContextMenu

To use a JavaFX ContextMenu you must first create a ContextMenu instance. Here is an example of creating a JavaFX ContextMenu:

ContextMenu contextMenu = new ContextMenu();

Add Menu Items to the ContextMenu

The ContextMenu needs one or more MenuItem instances which are displayed in the menu. To detect which menu item was clicked, you add listeners to the MenuItem instances. Here is an example of adding MenuItem instances to a ContextMenu:

ContextMenu contextMenu = new ContextMenu();

MenuItem menuItem1 = new MenuItem("Choice 1");
MenuItem menuItem2 = new MenuItem("Choice 2");
MenuItem menuItem3 = new MenuItem("Choice 3");

menuItem3.setOnAction((event) -> {
    System.out.println("Choice 3 clicked!");
});

contextMenu.getItems().addAll(menuItem1,menuItem2,menuItem3);

Notice how there is an onAction listener set on the third MenuItem, using setOnAction(), passing Java Lambda Expression as listener. If this MenuItem is clicked, the action listener is executed.

Add ContextMenu to JavaFX Control

A JavaFX ContextMenu needs to be attached to a JavaFX control to be active. You add a ContextMenu to a control via the setContextMenu() of a JavaFX control. Here is an example of adding a JavaFX ContextMenu to a JavaFX control:

TextArea textArea = new TextArea();

textArea.setContextMenu(contextMenu);

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next