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

[JFXPanel] Swing and JavaFX menus appear together in menu bar on Mac OS X

XMLWordPrintable

      Further to my earlier bug (RT-37362 ), I tried making my Swing/JavaFX application into a JavaFX application to get around the menubar issue. However, I have found a new problem: If you have a JavaFX menu with a MenuBar that has useSystemMenuBar set to true, and you have a Swing menu having called System.setProperty("apple.laf.useScreenMenuBar", "true"), then when you switch to the JavaFX window then the Swing window, the menus for *both* windows appear in the menu bar. Steps to reproduce:

      // A scene with a JavaFX menu that goes in the system menu bar:
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.VBox;


      public class MenuScene extends Scene
      {
          public MenuScene()
          {
              super(new VBox());
              MenuBar m = new MenuBar();
              Menu menu = new Menu("JavaFX");
              menu.getItems().add(new MenuItem("My item"));
              m.getMenus().add(menu);
              m.setUseSystemMenuBar(true);
              
              ((VBox)getRoot()).getChildren().addAll(m, new Label("Content"));
          }
      }


      // An application that makes a JavaFX window with a system menu bar and a Swing window with a system menu bar:
      import javafx.application.Application;
      import javafx.stage.Stage;

      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.SwingUtilities;


      public class TestMenuMixing extends Application {

          @Override
          public void start(Stage stage) throws Exception
          {
              // Primary has menu:
              stage.setScene(new MenuScene());
              stage.show();
              
              // Also show a Swing Window with menu:
              SwingUtilities.invokeLater(() -> {
                  System.setProperty("apple.laf.useScreenMenuBar", "true");
                  JFrame window = new JFrame();
                  JMenuBar menuBar = new JMenuBar();
                  menuBar.add(new JMenu("Swing"));
                  window.setJMenuBar(menuBar);
                  window.setVisible(true);
              });
          }
      }

      Run that code, and switch to the Swing window. It only shows "Swing" in the menu bar. Switch to the JavaFX window, and it shows "Swing" *and* "JavaFX" (it should only show "JavaFX").

            anthony Anthony Petrov (Inactive)
            nbrownjfx Neil Brown (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: