0

I have a JavaFX with 3 scenes, let's call them A, B, C. From scene A, I go to scene B by pressing a button. From scene B, I go to scene C in the same manner. Now comes the problem. In the C scene, I need the data from a text field that is in A scene. I tried creating a class member field in C controller and setting it from A scene but it doesn't work, since to go to scene C, I call the load() method in scene B ( so basically it's a new instance of C controller).

Ok, so I have the ResetPasswordPage.fxml with this corresponding controller:

public class ResetPasswordPageController {
private DatabaseAccess databaseAccess = new DatabaseAccess();
private String username;

@FXML
private TextField userFieldResetPasswordPage;

@FXML
void goToVerificationPage(ActionEvent event) {
    ResultSet resultSet = databaseAccess.executeSelectQuery("SELECT email FROM USER WHERE username = ?",
            new Object[] { userFieldResetPasswordPage.getText() });
    try {
        if (!resultSet.next()) {
            System.out.println("User doesn't exist");
            return;
        }

        String email = resultSet.getString("email");
        this.username = userFieldResetPasswordPage.getText();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("NewPasswordPage.fxml"));
        Parent parent = fxmlLoader.load();
        NewPasswordPageController newPasswordPageController = fxmlLoader.getController();
        newPasswordPageController.setUser(userFieldResetPasswordPage.getText()); // this is useless since I dont't
                                                                                    // go directly to
                                                                                    // NewPasswordPage, so I will
                                                                                    // have to call load method once
                                                                                    // again from the next screen

        int key = EmailSender.generateRandomKey();
        EmailSender.sendEmail(email, key);

        FXMLLoader fxmlLoader1 = new FXMLLoader(getClass().getResource("CodeVerificationPage.fxml"));
        Parent parent1 = fxmlLoader1.load();
        CodeVerificationPageController codeVerificationPageController = fxmlLoader1.getController();
        codeVerificationPageController.setGeneratedKey(key);
        Utilities.changeScene(event, parent1);

    } catch (SQLException | IOException | MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public String getUser() {
    return username;

}

The problem here is that I don't go directly from ResetPasswordPage to NewPasswordPage but there is an intermediary page (CodeVerificationPage), so if I set the field here in the ResetPasswordPage it will have no effect because the NewPasswordPage scene is created from CodeVerificationPage (the intermediary page)..

0 Answers0