Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8094248

[Swing, Mac, JFXPanel] Context menu with setOnContextMenuRequested doesn't work properly in Swing embedded JavaFX

    Details

      Description

      By executing the following code and following the steps:
      1- Right click on red panel -> context menu is shown (OK)
      2- Left click into the grey area -> context menu is hidden (OK)
      3- Right click on red panel -> context menu is not shown again (NOT OK)
      It should show the context menu.

      Note: Value of showingProperty is printed out.


      import javafx.application.Platform;
      import javafx.embed.swing.JFXPanel;
      import javafx.scene.Scene;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.StackPane;

      import javax.swing.*;
      import java.awt.*;

      public class ContextMenuTest {

          public static void main(String[] args) {

              javax.swing.SwingUtilities.invokeLater(() -> {
                  ContextMenuTest swingView = new ContextMenuTest();
                  swingView.createAndShowGUI();
              });

          }

          private void createAndShowGUI() {
              JFrame.setDefaultLookAndFeelDecorated(true);

              //Create and set up the window
              JFrame frame = new JFrame("Test Swing Frame");
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

              JPanel mainPanel = new JPanel();
              mainPanel.setMinimumSize(new Dimension(600, 400));
              mainPanel.setPreferredSize(mainPanel.getMinimumSize());

              //add JavaFX panel to Swing main panel
              final JFXPanel fxMainPanel = new JFXPanel();
              mainPanel.add(fxMainPanel, "0,0");

              Platform.setImplicitExit(false);

              // create JavaFX scene
              Platform.runLater(() -> createScene(fxMainPanel));

              frame.getContentPane().add(mainPanel);

              //Display the window
              frame.pack();
              frame.setVisible(true);
          }

          private void createScene(JFXPanel fxMainPanel) {

              StackPane rootPane = new StackPane();
              rootPane.setPrefSize(100.0, 100.0);
              rootPane.setStyle("-fx-background-color: red");
              ContextMenu contextMenu = new ContextMenu();
              MenuItem menuItem = new MenuItem("Test Menu Item");
              contextMenu.getItems().add(menuItem);

              rootPane.setOnContextMenuRequested(event -> {
                  contextMenu.show(rootPane, event.getScreenX(), event.getScreenY());
                  System.out.println("Context menu requested");
                  event.consume();
              });

              contextMenu.showingProperty().addListener((observable, oldValue, newValue) -> System.out.println("Context menu showing old = " + oldValue + ", new = " + newValue));

              Scene scene = new Scene(rootPane);
              fxMainPanel.setScene(scene);

          }
      }

        Attachments

          Activity

            People

            • Assignee:
              anthony Anthony Petrov (Inactive)
              Reporter:
              duke J. Duke (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: