I have root layout containing 2 layouts: - OptionsPane - DrawArea
What i am trying is to access DrawAreaController in OptionsPaneController to call its draw method. Below is initialize method from OptionsPaneController:
@Override
public void initialize(URL url, ResourceBundle rb) {
newDragonButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(newDragonButton.getText().equals("Nowy")){
//do something
}
else{
newDragonButton.setText("Nowy");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/DrawArea.fxml"));
DrawAreaController dac = (DrawAreaController) loader.getController();
Dragon dragon = new Dragon(600, 300, 20, 2, 90, 270, colorChooser.getValue());
if(dac == null)
System.out.println("controller = null");
dac.drawDragon(dragon);
}
}
});
}
Unfortunately dac is always null.
Is there any mistake in my code or is it impossible to load controller in another controller?
The controller is always null, because you never load the FXMLLoader.
The
load()
loads the fxml and instantiates the controller instance for you. If you do not use this method beforegetController()
, it will always returnnull
You may or may not want to store the root of the FXML..