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));
}
}
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));
}
}
- relates to
-
JDK-8087914 [JFXPanel] Clicking on Menu in JFXPanel ignored if another swing component has focus
-
- Resolved
-