0

Yes, i saw this answer Passing Parameters JavaFX FXML. But it's not working.

setDayParting works from Controller Nº1 but when i'm call updatePreview in Controller Nº2 the parameter what i passed is not there. Why this don't work?

  • Passing parameters in Controller Nº1:

    private void giveParameterToController() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(Main.newFileScreenFile));
        NewFileScreenController newFileScreenController;
        try {
            Pane pane = fxmlLoader.load();
            newFileScreenController = fxmlLoader.getController();
            newFileScreenController.setDayParting("Working");
            myController.setScreen(Main.newFileScreen);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
  • Controller Nº2:

public void setDayParting(String parting){
    dayParting = parting;
    System.out.println(dayParting);
}

private void updatePreview(){
    System.out.println(dayParting);
}
output:
Working
null
Community
  • 1
  • 1
exsnake
  • 1,767
  • 2
  • 23
  • 44
  • 1
    When is `updatePreview()` called? Do you ignore the value stored in your `pane` variable on purpose? – fabian Aug 08 '16 at 06:10
  • @fabian If i don't do the pane thing i cant have access to the controller, the updatePreview is called when I'm in the screen of controller 2. – exsnake Aug 08 '16 at 06:14
  • 1
    But in this case simply invoking `fxmlLoader.load()` without the assignment would also work. Furthermore, if you do not show this `Pane` either from the loading method or from the controller's `initialize` method, you probably use different scenes with different controller instances for displaying and for setting the data. – fabian Aug 08 '16 at 06:25
  • @fabian remove it but i still getting different instances of the controller... – exsnake Aug 08 '16 at 06:56
  • 1
    You need to pass the data to the controller that is created when you load the `Pane` that is actually shown on screen... – fabian Aug 08 '16 at 07:13
  • @fabian solved it, thanks you for the information. – exsnake Aug 08 '16 at 08:07

0 Answers0