1

I am building application which has different interfaces. I have decided to use the JavaFX for this and I wanted to know if I could have multiple containers saved in the FXML document and when using the the FXML Loader, pick a specific container by the id (i.e., fx:id).

I have recently come to know that several people grouped against me and make question duplicate. I will now state my defence and prove this is not duplicate.

What that question doesn't answer mine is the fact that I want to set fx:id for parent itself and use the fxmlloader to load parent on basis of id. Ok?

1 Answers1

2

You can do that using the FXML loader's getNamespace method.

(code from this SO answer, but see comments):

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
TextField foo = (TextField)loader.getNamespace().get("exampleFxId");

A better approach is to have separate fxml files for all your needs and then load each one as needed with the FXMLLoader.load() method.

Community
  • 1
  • 1
yeedle
  • 4,918
  • 1
  • 22
  • 22