I tried to use Swing Components in JavaFX with the SwingNode:
public class MyTest extends Application {
@Override
public void start(Stage stage) {
I've found an "awful" solution, but that works for me, repainting the Swing component in a delayed way, after showing the Stage:
new Timer().schedule(new TimerTask() {
public void run() {
swingNode.getContent().repaint();
}
}, 500L);
so, your code will look like this:
public class MyTest extends Application {
@Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
FlowPane pane = new FlowPane();
Button btn = new Button("1");
btn.setVisible(false);
pane.getChildren().add(btn);
createAndSetSwingContent(swingNode);
pane.getChildren().add(swingNode);
stage.setScene(new Scene(pane, 100, 50));
stage.show();
btn.setVisible(true);
// delays Swing component repaint 1/2 sec.
new Timer().schedule(new TimerTask() {
public void run() {
swingNode.getContent().repaint();
}
}, 500L);
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent(new JButton("Click me!"));
}
});
}
public static void main(String[] args) {
launch(args);
}
}
A bit late, but I hope it helps!
objSwingNode.requestFocus(); objSwingNode.autosize();
The Problem is an known bug in java 8 update 31.
(https://javafx-jira.kenai.com/browse/RT-37810)
It is fixed in java 8 update 40.