Good day to all!

When working with javaFX, in particular using Scene Builder, I encountered the following problem ...

Given:

  1. There is an fxml file containing an anchor pane (fxml is obtained from the Scene Builder);
  2. For Anchor Pane is not set Controller Class.
  3. This fxml is loaded into the Java Application using FXMLLoader.

It is necessary:

  1. After downloading this very Anchor Pane set it to the value of the Controller Class. (something like setControllerClass)
  2. This is necessary in order to load the same fxml, but hang different handlers on them.

Question: Is it possible, and if so, how is it implemented?

Thank you in advance!

    1 answer 1

    The solution turned out to be quite simple - the controller class is installed before loading fxml into the FXMLLoader loader itself:

    FXMLLoader fxmlLoader = new FXMLLoader(fxmlURL); MyClassController controller = new MyClassController(); fxmlLoader.setController(controller); fxmlLoader.load(); AnchorPane pane = fxmlLoader.getRoot();