0

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.

2
  • What is the problem with root.getchidren.add(close) ? Commented Mar 17, 2013 at 17:46
  • @gontrad When i try to write root.getChildren.add(close) then it gives me error msg that "get children has protected access in Parent". Commented Mar 17, 2013 at 20:38

1 Answer 1

2

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

AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource(“table.fxml”));
root.getChidren.add(close);
Sign up to request clarification or add additional context in comments.

3 Comments

This can't be the solution without a modification of the fxml file since the rootis the same instance.
You just need to choose same layout class as one in fxml file
Also in most cases Pane will work as it's a parent for everything except Group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.