i wrote an simple app in JavaFx and wanna use swingNode in it,So i had a problem with change the size or position of my node
public class SwingFx extends Application {
@Override
public void start (Stage stage) {
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 250, 150));
stage.show();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton jButton = new JButton("Click me!");
jButton.setBounds(0,0,5,5);
swingNode.setContent(jButton);
}
});
}
}
i set the bounds of the JButton but it doesn't work , when i run the app the button appear in the center and in it's default width and height
you should use a parent container and set its layout to null if you want setbounds()
work
public class SwingFx extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start (Stage stage) {
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 250, 150));
stage.show();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton jButton = new JButton("Click me!");
jButton.setBounds(10,10,50,50);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(jButton);
swingNode.setContent(panel);
}
});
}
}