Using the FXMLLoader API

Aside from only loading and FXML, you may need to use other FXMLLoader functionality. In this case, you need to construct an object:

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));loader.load();FirstController controller = loader.getController();Parent root = loader.getRoot();
Due to an unfortunate name choice, there are several overridden load() methods in FXML and most of them are static. Make sure you are using non-static methods for calls from the instantiated FXMLLoader. See the following example: FXMLLoader loader = new FXMLLoader(); loader.load(getClass().getResource("FXMLDocument.fxml")); // STATIC method! FirstController controller = loader.getController(); // returns null ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.