Add image from java using fxml file created in scene builder


Question

If I want to add image node to fxml (created in scene builder) file from java, how can I achieve this ?

private ImageView close;
private ImageView close;
Image i = new Image(“file:C:\\Users\\Tarun\\Desktop\\Close-icon.png”);
close = new ImageView();
close.setImage(i);
close.setFitHeight(20);
close.setFitWidth(20);
close.setX(570);
close.setY(10);
Parent root = FXMLLoader.load(getClass().getResource(“table.fxml”));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

I know that there is some method root.getchidren.add(close) but here I am not able to implement this method.

1
0
3/17/2013 4:07:59 PM

Accepted Answer

You need to cast Parent to an appropriate class. E.g

AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource(“table.fxml”));
root.getChidren.add(close);
2
3/18/2013 8:04:38 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon