JavaFX - setText () not working

I can not set the text of the text field. There is no error, but the text box is still empty. The rest of the program works, the method is called, and System.out.println (...) prints the correct text. Therefore, the problem is that the text of the text field cannot be set. Even if I just write textField.setText ("0"); the text box is still empty. When I set the text for a text field under public void initialize (...), it also works. So why doesn't it work in setCurrentInfo?

@FXML
private TextField textField;

private Info currentInfo;

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

public void setCurrentInfo(Info currentInfo) {
    textField.setText(currentInfo.getpw1());
    System.out.println(currentInfo.getpw1());
    this.currentInfo = currentInfo;
}

The part of the controller where setCurrentInfo is called:

@FXML
private void handleBtn1(ActionEvent event) throws Exception{
    Info info = new Info(textFieldA.getText(), textFieldB.getText());
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("FXMLPassword.fxml"));
    loader.load();
    Stage stage; 
    Parent root;
    if(event.getSource()==btn1){
        //get reference to the button stage         
        stage=(Stage) btn1.getScene().getWindow();
        //load up OTHER FXML document
        root = FXMLLoader.load(getClass().getResource("FXMLPassword.fxml"));
    }
    else{
        stage=(Stage) btn1.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    }
    //create a new scene with root and set the stage
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    FXMLPasswordController passwordController = loader.getController();
    passwordController.setCurrentInfo(info);
    }
+4
source share
1 answer

FXMLLoader. , :

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLPassword.fxml"));
loader.load();

FXMLPasswordController passwordController = loader.getController();

FXMLLoader FXMLPassword.fxml. loader.load(), fxml , . , , loader.load(), .

, loader , , TextField .

TextField

root = FXMLLoader.load(getClass().getResource("FXMLPassword.fxml"));

FXMLLoader.load(...) , .

:

@FXML
private void handleBtn1(ActionEvent event) throws Exception{
    Info info = new Info(textFieldA.getText(), textFieldB.getText());
    Stage stage; 
    Parent root;
    if(event.getSource()==btn1){
        //get reference to the button stage         
        stage=(Stage) btn1.getScene().getWindow();
        //load up OTHER FXML document
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("FXMLPassword.fxml"));
        root = loader.load();
        FXMLPasswordController passwordController = loader.getController();
        passwordController.setCurrentInfo(info);
    }
    else{
        stage=(Stage) btn1.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    }
    //create a new scene with root and set the stage
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
+3

Source: https://habr.com/ru/post/1624633/


All Articles