While trying to load an FXML file, one usually does something like the following:
FXMLLoader loader = FXMLLoader.load(getClass().getResource("File.fxml"));
Region root = loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
However, as I tried to put the loading code into the controller for "caller convenience" I did the following:
public Controller()
{
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
loader.setController(this);
Parent root = loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
This worked quite good, because now I just had to call the constructor to create the new window.
But I had to remove the
fx:controller="package.Class"
attribute in the FXML file, because otherwise an Exception ("javafx.fxml.LoadException: controller is already set") was thrown when I invoked the
fxmlloader.setController(this);
method in the constructor. Since I'm using NetBeans and its "Make Controller" feature (right-click on the FXML file), the controller class can't be created because of the missing attribute.
Summary:
What I want to achieve is a way to have the "fx:controller" attribute still set in the FXML (for NetBeans) and also being able to load the FXML conveniently inside the Controller class.
Is this possible, or do I need some kind of wrapper class which does create the FXML window(s)?
Thanks in advance.