0

I have pretty simple application (i just want to understand controller switching mechanism). First window shows label and button. When you click the button, another window with button will show. Now when you click this second button, Label in first window should change. I have read some posts here, also tried this one Java FX change Label text in a previous stage scene, however, with no success. If you could explain it to me on this simple example, maybe i will be able to better understand controllers logic. Here is my code, thank for any help:

PrimaryController with label to be changed and the button, which opens new window

public class PrimaryController implements Initializable {

@FXML
private Label label;

@FXML
private Button btnButton;

@Override
public void initialize(URL url, ResourceBundle rb) {
    btnButton.setOnAction((event) -> {

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("secondaryFXML.fxml"));
            Parent root = loader.load();
            loader.<SecondaryController>getController().setPrimaryController(this);
            Stage stage = new Stage();
            Scene scene = new Scene(root);

            stage.setScene(scene);
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(PrimaryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    });        
}    

public void setLabelText(String string){
    label.setText(string);
}

}

Secondary Controller with change label button

public class SecondaryController implements Initializable {

@FXML
private Button btnChangeLabel;

private PrimaryController primary;

@Override
public void initialize(URL url, ResourceBundle rb) {

    btnChangeLabel.setOnAction((event) -> {            
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("primaryFXML.fxml"));
            loader.load();
            PrimaryController primaryCtrl = loader.<PrimaryController>getController();
            primaryCtrl.setLabelText("eres");

        } catch (IOException ex) {
            Logger.getLogger(SecondaryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    });
}

public void setPrimaryController(PrimaryController primary){
    this.primary = primary;
}

}

Mr. Crow
  • 27
  • 12

1 Answers1

0

In the button's action event handler, you're loading the FXML file again (creating new instances of all the controls defined there) and getting the controller for the new UI. Then you call setLabelText(...) on the controller for that UI, changing the text in the newly-created label. You haven't ever displayed the UI you loaded from the FXML file, so this will have no visible effect.

Of course, you don't actually want a new instance of all your controls: you (presumably) want to change the text of the label that is already displayed. You have already passed a reference to the existing PrimaryController instance to the SecondaryController instance, so just call setLabelText(...) on that:

@Override
public void initialize(URL url, ResourceBundle rb) {
    btnChangeLabel.setOnAction((event) ->             
        primary.setLabelText("eres"));
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks for answer, but unfortunately, this is not working for me :( – Mr. Crow May 25 '17 at 15:20
  • @Mr.Crow "Not working" is not a problem statement. What happens? – James_D May 25 '17 at 15:25
  • NOthing in console, program acts like no code is set to run on button click. – Mr. Crow May 25 '17 at 15:31
  • @Mr.Crow I assume you mean you added a `System.out.println(...)` to the event handler, and didn't see anything. That would indicate something is wrong in your FXML file. Is the `initialize()` method getting called? – James_D May 25 '17 at 15:33
  • there was a bad controller assigned to fxml file - i have changed controller name but not reference in fxml file. now it works – Mr. Crow May 25 '17 at 15:40