Java FX change label in previous stage

2019-08-24 10:21发布

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;
}

}

1条回答
一夜七次
2楼-- · 2019-08-24 10:49

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"));
}
查看更多
登录 后发表回答