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

Menu inside JFXPanel doesn't hide.

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Encountered on Windows 10, using OpenJDK 11.0.10.9 with JavaFX 17.0.0.1

      A DESCRIPTION OF THE PROBLEM :
      When a Menu is embedded in a JFXPanel, clicking on it does not hide it. Instead, it sometimes blink as if closing then immediately reopening.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Executing the provided code will open a window. Clicking any of the menus will open it, clicking again the same menu will not close it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The menu should be closed. It works correctly when using pure JavaFX (available by replacing the call to useJFXPanel with the one to usePrimaryStage).
      ACTUAL -
      The menu stays open, and sometimes blinks briefly, as if closed and reopened right after.

      ---------- BEGIN SOURCE ----------
      package testjavafx;

      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.embed.swing.JFXPanel;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.Pane;
      import javafx.scene.layout.Region;
      import javafx.stage.Stage;

      public class TestMenuJavaFX extends Application {

      private static final String TITLE = "test menu JavaFX";


      @Override
      public void start(Stage primaryStage) {
      MenuBar menuBar = new MenuBar(
      new Menu("Menu 1", null,
      new MenuItem("Menu item 1-1"),
      new MenuItem("Menu item 1-2")),
      new Menu("Menu 2", null,
      new MenuItem("Menu item 2-1"),
      new MenuItem("Menu item 2-2")),
      new Menu("Menu 3", null,
      new MenuItem("Menu item 3-1"),
      new MenuItem("Menu item 3-1")));
      menuBar.setPrefWidth(300);
      Region root = new Pane(menuBar);
      root.setPrefSize(300, 185);
      useJFXPanel(root);
      //usePrimaryStage(primaryStage, root);
      }

      private static void useJFXPanel(Region root) {
      JFXPanel jfxPanel = new JFXPanel();
      jfxPanel.setScene(new Scene(root));
      SwingUtilities.invokeLater(() -> {
      JFrame jFrame = new JFrame(TITLE);
      jFrame.setSize((int) root.getWidth(), (int) root.getHeight());
      jFrame.add(jfxPanel);
      jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jFrame.setLocationRelativeTo(null);
      jFrame.setVisible(true);
      });
      }

      private static void usePrimaryStage(Stage primaryStage, Parent root) {
      primaryStage.setScene(new Scene(root));
      primaryStage.show();
      }

      public static void main(String[] args) {
      launch(TestMenuJavaFX.class, args);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Adding this block to the method start before calling useJFXPanel will apparently fix the issue:

      menuBar.getMenus().forEach(menu -> {
      menu.addEventHandler(Menu.ON_HIDING, e -> {
      Platform.runLater(menu::hide);
      });
      });

      FREQUENCY : always


        1. Capture.pptx
          4.69 MB
          Praveen Narayanaswamy
        2. TestMenuJavaFX.java
          2 kB
          Praveen Narayanaswamy

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: