0

I have seen this question asked a few times but unfortunately I couldn't get my problem solved from the given answers. This suits me best, easy to understand.

I have 1 main, 2 controllers(primary, secondary) and 2 fxml(primary,secondary) files. This is how I open my second window from my primary controller:

@FXML
private void handleButtonOpenSecondWindow(ActionEvent event) {

    try{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SecondWindow.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL); 
        stage.setTitle("Second Window");
        stage.setScene(new Scene(root1));  
        stage.show();
      }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

This works fine, all I want above is to add my object(s) somewhere in the parenthesises above.

Maybe:

root1.setMyObjects(object1, object2);

I tried to convert my code to the answer I liked but when I create a constructor for the secondary Controller the Parent root1 = (Parent) fxmlLoader.load(); line always fails(But it works when called from the primary controller). I do have an initialize(); method as well in secondary controller. I tried with constructor, without constructor, putting constructor parts into initialize(); but none worked... I would be glad if you can give a hand.

Community
  • 1
  • 1
Anarkie
  • 657
  • 3
  • 19
  • 46

1 Answers1

0

You should use global variables in first controller, and get variables in second controller.