If I embed Swing into JavaFX which is embedded into Swing I do have issues with the focus of controls.
Lets start with this example:
public class EmbeddedTestWithFX extends JFrame {
public EmbeddedTestWithFX() {
setSize(new Dimension(300, 300));
JTextArea textArea = new JTextArea();
textArea.setSize(new Dimension(150, 150));
JPanel container = new JPanel();
container.setLayout(null);
container.add(textArea);
JFXPanel jfxPanel = new JFXPanel();
Platform.runLater(() -> {
SwingNode swing = new SwingNode();
StackPane stack = new StackPane(swing);
Scene scene = new Scene(stack);
swing.setContent(container);
SwingUtilities.invokeLater(() -> jfxPanel.setScene(scene));
});
setContentPane(jfxPanel);
}
public static void main(String[] args) {
new EmbeddedTestWithFX().setVisible(true);
}
}
I can see the TextArea but the issue is, It doesn't get the focus by clicking on it (btw. it's the same for other controls like jButtons). If I register a FocusListener on the JTextArea, I see that it becomes the focus after a click, but it loses the focus instantly.
But if I use:
-Djavafx.embed.singleThread=true
the example starts to work. I can click now the TextArea, it holds the focus and I can enter text.
Lets start with this example:
public class EmbeddedTestWithFX extends JFrame {
public EmbeddedTestWithFX() {
setSize(new Dimension(300, 300));
JTextArea textArea = new JTextArea();
textArea.setSize(new Dimension(150, 150));
JPanel container = new JPanel();
container.setLayout(null);
container.add(textArea);
JFXPanel jfxPanel = new JFXPanel();
Platform.runLater(() -> {
SwingNode swing = new SwingNode();
StackPane stack = new StackPane(swing);
Scene scene = new Scene(stack);
swing.setContent(container);
SwingUtilities.invokeLater(() -> jfxPanel.setScene(scene));
});
setContentPane(jfxPanel);
}
public static void main(String[] args) {
new EmbeddedTestWithFX().setVisible(true);
}
}
I can see the TextArea but the issue is, It doesn't get the focus by clicking on it (btw. it's the same for other controls like jButtons). If I register a FocusListener on the JTextArea, I see that it becomes the focus after a click, but it loses the focus instantly.
But if I use:
-Djavafx.embed.singleThread=true
the example starts to work. I can click now the TextArea, it holds the focus and I can enter text.
- relates to
-
JDK-8089604 JDialog-JFXPanel-SwingNode-JPanel-JButton mouse events ignored
- Closed
-
JDK-8136530 Heavy CPU usage when mixing JavaFX and Swing multiple times
- Closed