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!
FXMLLoader
, right? So I don't see a benefit in waiting for theinitialize()
- method. As soon as the FXML is loaded, the following code has access to the@FXML
variables. Sure, he does it in the start method and not in the constructor, but wouldinitialize()
bring any benefit in his case? – Ilanailangilang