mydomain
No ADS
No ADS

JavaFX ContextMenu Tutorial with Examples

  1. JavaFX ContextMenu
  2. ContextMenu example
  3. Accelerator and item types
  4. ContextMenu and events

1. JavaFX ContextMenu

When you cannot allocate any space of your user interface for a required functionality, you can use a context menu. A context menu is a pop-up window that appears in response to a right mouse click. A context menu can contain one or more menu items.
Note that the ContextMenu is quite similar to a Menu, you can add items with types of MenuItem, CheckMenuItem, RadioMenuItem, SeparatorMenuItem to ContextMenu.

2. ContextMenu example

No ADS
The simple example below illustrates a ContextMenu which will display when users right-click on a circle.
ContextMenuDemo.java
package org.o7planning.javafx.contextmenu;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ContextMenuDemo extends Application {

    @Override
    public void start(Stage stage) {

        Label label = new Label();

        Circle circle = new Circle();
        circle.setRadius(80);
        circle.setFill(Color.AQUA);

        VBox root = new VBox();
        root.setPadding(new Insets(5));
        root.setSpacing(5);

        root.getChildren().addAll(label, circle);

        // Create ContextMenu
        ContextMenu contextMenu = new ContextMenu();

        MenuItem item1 = new MenuItem("Menu Item 1");
        item1.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                label.setText("Select Menu Item 1");
            }
        });
        MenuItem item2 = new MenuItem("Menu Item 2");
        item2.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                label.setText("Select Menu Item 2");
            }
        });

        // Add MenuItem to ContextMenu
        contextMenu.getItems().addAll(item1, item2);

        // When user right-click on Circle
        circle.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {

            @Override
            public void handle(ContextMenuEvent event) {

                contextMenu.show(circle, event.getScreenX(), event.getScreenY());
            }
        });

        Scene scene = new Scene(root, 400, 200);

        stage.setTitle("JavaFX ContextMenu (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

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

}

3. Accelerator and item types

Like Menu, you can add items with types of MenuItem, CheckMenuItem, RadioMenuItem, SeparatorMenuItem to ContextMenu. You can also attach Accelerators to items, users can use keyboard shortcuts instead of clicking on that item.
ContextMenuDemo2.java
package org.o7planning.javafx.contextmenu;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ContextMenuDemo2 extends Application {

    @Override
    public void start(Stage stage) {

        Circle circle = new Circle();
        circle.setRadius(80);
        circle.setFill(Color.AQUA);

        VBox root = new VBox();
        root.setPadding(new Insets(5));
        root.setSpacing(5);

        root.getChildren().addAll(circle);

        // Create ContextMenu
        ContextMenu contextMenu = new ContextMenu();

        MenuItem menuItem = new MenuItem("Menu Item");

        // Set accelerator to menuItem.
        menuItem.setAccelerator(KeyCombination.keyCombination("Ctrl+O"));

        Menu parentMenu = new Menu("Parent");
        MenuItem childMenuItem1 = new MenuItem("Child 1");
        MenuItem childMenuItem2 = new MenuItem("Child 2");
        parentMenu.getItems().addAll(childMenuItem1, childMenuItem2);

        CheckMenuItem checkMenuItem = new CheckMenuItem("Check Menu Item");
        checkMenuItem.setSelected(true);

        SeparatorMenuItem separatorMenuItem = new SeparatorMenuItem();

        RadioMenuItem radioMenuItem1 = new RadioMenuItem("Radio - Option 1");
        RadioMenuItem radioMenuItem2 = new RadioMenuItem("Radio - Option 2");
        ToggleGroup group = new ToggleGroup();

        radioMenuItem1.setToggleGroup(group);
        radioMenuItem2.setToggleGroup(group);

        // Add MenuItem to ContextMenu
        contextMenu.getItems().addAll(menuItem, parentMenu, checkMenuItem, //
                separatorMenuItem, radioMenuItem1, radioMenuItem2);

        // When user right-click on Circle
        circle.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {

            @Override
            public void handle(ContextMenuEvent event) {

                contextMenu.show(circle, event.getScreenX(), event.getScreenY());
            }
        });

        Scene scene = new Scene(root, 400, 200);

        stage.setTitle("JavaFX ContextMenu (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

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

}

4. ContextMenu and events

No ADS
Some events of ContextMenu can be useful to you. For example, onShowing is the method to handle event happens before contextMenu starts displaying. You can take advantage of this method to do something, for example, add MenuItems to ContextMenu.
// Called just prior to the ContextMenu being shown.
public final void setOnShowing(EventHandler<WindowEvent> value)

// Called just after the ContextMenu is shown.
public final void setOnShown(EventHandler<WindowEvent> value)

// Called just prior to the ContextMenu being hidden.
public final void setOnHiding(EventHandler<WindowEvent> value)

// Called just after the ContextMenu has been hidden.
public final void setOnHidden(EventHandler<WindowEvent> value)

// Called when there is an external request to close this
public final void setOnCloseRequest(EventHandler<WindowEvent> value)
ContextMenuEventDemo.java
package org.o7planning.javafx.contextmenu;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class ContextMenuEventDemo extends Application {

  private int index = 1;

  @Override
  public void start(Stage stage) {

      TextArea textArea = new TextArea();
      textArea.setMinHeight(100);

      Circle circle = new Circle();
      circle.setRadius(80);
      circle.setFill(Color.GRAY);

      VBox root = new VBox();
      root.setPadding(new Insets(5));
      root.setSpacing(5);

      root.getChildren().addAll(textArea, circle);

      // Create ContextMenu
      ContextMenu contextMenu = new ContextMenu();
      MenuItem menuItem = new MenuItem("Menu Item");
      contextMenu.getItems().add(menuItem);

      // When user right-click on Circle
      circle.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {

          @Override
          public void handle(ContextMenuEvent event) {

              contextMenu.show(circle, event.getScreenX(), event.getScreenY());
          }
      });
      // Called just prior to the ContextMenu being shown.
      // Add new MenuItem to ContextMenu.
      contextMenu.setOnShowing(new EventHandler<WindowEvent>() {

          @Override
          public void handle(WindowEvent event) {
              textArea.appendText("On Showing..\n");

              MenuItem menuItem = new MenuItem("New Menu Item " + index++);
              contextMenu.getItems().add(menuItem);
          }
      });

      // Called just after the ContextMenu is shown.
      contextMenu.setOnShown(new EventHandler<WindowEvent>() {

          @Override
          public void handle(WindowEvent event) {
              textArea.appendText("On Show..\n");
          }
      });
   
      // Called just prior to the ContextMenu being hidden.
      contextMenu.setOnHiding(new EventHandler<WindowEvent>() {

          @Override
          public void handle(WindowEvent event) {
              textArea.appendText("On Hiding..\n");
          }
      });
   
      // Called just after the ContextMenu has been hidden.
      contextMenu.setOnHidden(new EventHandler<WindowEvent>() {

          @Override
          public void handle(WindowEvent event) {
              textArea.appendText("On Hidden..\n");
          }
      });

      Scene scene = new Scene(root, 400, 250);

      stage.setTitle("JavaFX ContextMenu (o7planning.org)");
      stage.setScene(scene);
      stage.show();
  }

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

}
No ADS

JavaFX Tutorials

Show More
No ADS