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

SwingNode on Linux prevents new Swing window getting focus

XMLWordPrintable

    • x86
    • linux_ubuntu

      FULL PRODUCT VERSION :
      java version "1.8.0_91"
      Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
      Java HotSpot(TM) Server VM (build 25.91-b14, mixed mode)

      AND:

      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+115)
      Java HotSpot(TM) Server VM (build 9-ea+115, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux banshee 3.13.0-85-generic #129-Ubuntu SMP Thu Mar 17 20:50:41 UTC 2016 i686 i686 i686 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      Ubuntu-only: If you make an FX window with a SwingNode component, and then display a pure-Swing window, the new Swing window will not receive focus, and clicking on components in the new window will not give them focus. No such problem exists on Windows or Mac.

      Other hints: switching focus to another application (or sometimes to the parent window and back) will fix the issue. Generating two new windows may let you focus the first window, and then afterwards focus the second window. Removing the SwingNode from the FX window after showing the Swing window seems to remove the problem.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the code below. Either use the menu command or click the button to show a new window with a text field. It will not be focused (on Ubuntu), and clicking on it will not focus it.

      /////////////////////////////////////
      import javax.swing.*;
      import javafx.application.Application;
      import javafx.embed.swing.SwingNode;
      import javafx.scene.Scene;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class TestSwingNodeWindow extends Application
      {
          @Override
          public void start(Stage primaryStage) throws Exception
          {
              SwingNode swingNode = new SwingNode();
              BorderPane root = new BorderPane(swingNode);
              SwingUtilities.invokeAndWait(() -> {
                  JPanel panel = new JPanel();
                  JButton button = new JButton("Open window");
                  button.addActionListener(e -> {
                      showSwingTextFieldWindow();
                      // This makes it work (but not a realistic solution):
                      //Platform.runLater(() -> root.getChildren().clear());
                  });
                  panel.add(button);

                  swingNode.setContent(panel);
              });
              MenuItem menuItem = new MenuItem("Show window");
              menuItem.setOnAction(e -> SwingUtilities.invokeLater(this::showSwingTextFieldWindow));
              root.setTop(new MenuBar(new Menu("Menu", null, menuItem)));
              primaryStage.setScene(new Scene(root, 400, 400));
              primaryStage.show();
          }

          private void showSwingTextFieldWindow()
          {
              JFrame window = new JFrame("The new window");
              window.add(new JTextField("text field"));
              window.pack();
              window.setVisible(true);
          }
      }
      ////////////////////////////////

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect that when the new window is shown: (a) it should already put focus in the text field, and even if not (b) clicking the text field should focus the text field. On Windows and Mac I do get (a), on Ubuntu neither (a) nor (b) works.
      ACTUAL -
      On Ubuntu, clicking the text field does nothing. If you add a mouse click listener, it is receiving clicks, but it does not get focused.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javafx.application.Application;
      import javafx.embed.swing.SwingNode;
      import javafx.scene.Scene;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class TestSwingNodeWindow extends Application
      {
          @Override
          public void start(Stage primaryStage) throws Exception
          {
              SwingNode swingNode = new SwingNode();
              BorderPane root = new BorderPane(swingNode);
              SwingUtilities.invokeAndWait(() -> {
                  JPanel panel = new JPanel();
                  JButton button = new JButton("Open window");
                  button.addActionListener(e -> {
                      showSwingTextFieldWindow();
                      // This makes it work (but not a realistic solution):
                      //Platform.runLater(() -> root.getChildren().clear());
                  });
                  panel.add(button);

                  swingNode.setContent(panel);
              });
              MenuItem menuItem = new MenuItem("Show window");
              menuItem.setOnAction(e -> SwingUtilities.invokeLater(this::showSwingTextFieldWindow));
              root.setTop(new MenuBar(new Menu("Menu", null, menuItem)));
              primaryStage.setScene(new Scene(root, 400, 400));
              primaryStage.show();
          }

          private void showSwingTextFieldWindow()
          {
              JFrame window = new JFrame("The new window");
              window.add(new JTextField("text field"));
              window.pack();
              window.setVisible(true);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only reliable work-around I've found is to remove the SwingNode, but that is not realistic. I would love a reliable work-around.

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

              Created:
              Updated:
              Resolved: