1
votes

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
What do you mean with container? Do you mean the controller? - fireandfuel
@fireandfuel For example, Anchor Pane. Or Grid Pane. Like those. - Bomba Bazooka
@yeedle yup, it seems very similar. However I think he/she/they want to ask how to load the parent itself with an id. - Adit Kirtani
@RahulSrinath YES you get me! That is what I want. btw I am a "he" - Bomba Bazooka

1 Answers

2
votes

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.