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

[JFXPanel] scene doesn't gain focus when node programatically focused

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • tbd
    • 8, 9
    • javafx

      When requesting focus to a javafx node which is inside a JFXPanel when the jfxpanel is not the focus owner of the Swing Focus Manager, the JFXPanel is not focused. This cause the javafx node to presented as is didn't receive the focus even when the node is marked as the focus owner in the scene.

      Example:
      1. Press the swing button
      The text field is not presented as focused.

      import java.awt.BorderLayout;
      import java.awt.Dimension;

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

      import javafx.application.Platform;
      import javafx.beans.InvalidationListener;
      import javafx.embed.swing.JFXPanel;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.VBox;

      public class SampleFocusApp extends JFrame
      {
        private TextField textField;

        public SampleFocusApp()
        {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLayout(new BorderLayout());
          final JFXPanel panel = new JFXPanel();
          panel.setPreferredSize(new Dimension(200, 200));
          add(panel, BorderLayout.CENTER);
          JButton button = new JButton("Button");
          button.addActionListener(e -> Platform.runLater(() -> textField.requestFocus()));

          add(button, BorderLayout.SOUTH);

          Platform.runLater(() -> {
            textField = new TextField("Text");
            VBox root = new VBox(new Button("FX Button"), textField);
            final Scene scene = new Scene(root);
            panel.setScene(scene);

            scene.focusOwnerProperty().addListener((InvalidationListener) observable -> {
              System.out.println(scene.getFocusOwner());
            } );

            SwingUtilities.invokeLater(() -> {
              button.requestFocusInWindow();
            } );
          } );
          pack();
        }

        public static void main(String[] args)
        {
          SwingUtilities.invokeLater(() -> (new SampleFocusApp()).setVisible(true));
        }

      }




            psadhukhan Prasanta Sadhukhan
            gpererajfx Gayan Perera (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: