My Application
class looks like this:
public class Test extends Application {
private static Logger logger = LogManager.getRootLogger();
@Override
public void start(Stage primaryStage) throws Exception {
String resourcePath = "/resources/fxml/MainView.fxml";
URL location = getClass().getResource(resourcePath);
FXMLLoader fxmlLoader = new FXMLLoader(location);
Scene scene = new Scene(fxmlLoader.load(), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The FXMLLoader
creates an instance of the corresponding controller (given in the FXML
file via fx:controller
) by invoking first the default constructor and then the initialize
method:
public class MainViewController {
public MainViewController() {
System.out.println("first");
}
@FXML
public void initialize() {
System.out.println("second");
}
}
The output is:
first
second
So, why does the initialize
method exist? What is the difference between using a constructor or the initialize
method to initialize the controller required things?
Thanks for your suggestions!
In Addition to the above answers, there probably should be noted that there is a better way to implement the initialization. There is an interface called Initializable from the fxml library.
Parameters:
And the note of the docs why the simple way of using
@FXML public void initialize()
works:NOTE
This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible.In a few words: The constructor is called first, then any
@FXML
annotated fields are populated, theninitialize()
is called. So the constructor does NOT have access to@FXML
fields referring to components defined in the .fxml file, whileinitialize()
does have access to them.Quoting from the Introduction to FXML:
The
initialize
method is called after all@FXML
annotated members have been injected. Suppose you have a table view you want to populate with data: