Javafx textfield settext does not work

Asked

Viewed 164 times

0

I have a registration screen, using FXML, and want to reuse this screen for the change button.

But when I do:

txtNome.setText("ola");
System.out.println(txtNome.getText());

Opens the registration screen, with the field Nome blank, console returns "ola" and returns no error.

Code that opens the registration screen:

public void newPage(String path){

        try {
            Stage stage;
            Parent root;
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getClassLoader().getResource(path));
            root = fxmlLoader.load();
            stage = new Stage();
            stage.initModality(Modality.APPLICATION_MODAL);
            txtNome = new TextField();
            txtNome.setText("ola");
            System.out.println(txtNome.getText());
            stage.setScene(new Scene(root, 900, 900));
            stage.showAndWait();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

1 answer

0

Good as you did not give more details of your code, I imagine you want when you click the change button to open a screen in modal already with information as for example: username?

if yes, follow a code:

    public void newPage(String path) {

    try {

        FXMLLoader root = new FXMLLoader(getClass().getResource(path));
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        ControllerModal controllerModal = new ControllerModal("teste texto");
        root.setController(controllerModal);
        Scene scene = new Scene(root.load());

        stage.setScene(scene);

        stage.showAndWait();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Controllermodal class:

public class ControllerModal implements Initializable{

@FXML
private Label texto;

private String testeTexto;

public ControllerModal(String testeTexto) {
    this.testeTexto = testeTexto;
}


public void initialize(URL location, ResourceBundle resources) {
    texto.setText(testeTexto);
}}

Explanation:

Well, with this code you will need to create a new controller class for this modal, thus following the MVC standard (this way you should not reuse the same FXML).

If that’s not what you wanted, leave more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.