The following code produces a JFrame
containing a JFXPanel
and a JPanel
. Each of the panels contains a text field.
JFXPanel fxPanel = new JFXPanel();
JPanel swingPanel = new JPanel(new FlowLayout());
swingPanel.add(new JTextField("Swing"));
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.add(fxPanel, BorderLayout.PAGE_START);
contentPanel.add(swingPanel, BorderLayout.CENTER);
JFrame frame = new JFrame("Main Frame");
frame.setContentPane(contentPanel);
FlowPane root = new FlowPane();
root.getChildren().add(new TextField("FX"));
Scene scene = new Scene(root);
Platform.runLater(() -> fxPanel.setScene(scene));
SwingUtilities.invokeLater(() -> frame.setVisible(true));
Suppose we start with the Swing text field focused. Then suppose I click inside the JFXPanel
(but not within the bounds of its text field). The JFXPanel
gives focus to the TextField
.
Why does this happen? Why does the JFXPanel not keep its own focus? Why does it give it to the text field? How does it choose which of its components to give focus to? What is the correct way to prevent it giving its focus to the text field?