JavaFX FXML controller – constructor vs initialize method

In a few words: The constructor is called first, then any @FXML annotated fields are populated, then initialize() is called.

This means the constructor does not have access to @FXML fields referring to components defined in the .fxml file, while initialize() does have access to them.

Quoting from the Introduction to FXML:

[…] the controller can define an initialize() method, which will be called once on an implementing controller when the contents of its associated document have been completely loaded […] This allows the implementing class to perform any necessary post-processing on the content.

Leave a Comment