1

I have a login page and I'm trying to get a users email that they entered to display on the next page which is their homepage. This is that code:

            FXMLLoader loaderNew = new FXMLLoader(getClass().getResource("/sample/view/Dashboard.fxml"));
            try {
                loaderNew.load();
            } catch (IOException e) {
                e.printStackTrace();
            }
            DashboardController dashboardController = loaderNew.getController();
            dashboardController.setName(loginEmail.getText().trim());

And this is the method I'm trying to call:

public void setName(String name){
    nameLbl.setText(name);
}

I'm getting no errors, it's just not updated the nameLbl and I have no idea why. The logincontroller will go the dashboard successfully, it just won't update the nameLbl.

Here are both full codes.

Login Controller:

package sample.controller;

import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import sample.database.DBConnection;
import sample.model.User;

public class LoginController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button loginButton;

    @FXML
    private TextField loginEmail;

    @FXML
    private Button createAccountSwitchButton;

    @FXML
    private Label errorLabel;

    @FXML
    private PasswordField loginPassword;

    private DBConnection dbConnection;

    @FXML
    void initialize() {

        dbConnection = new DBConnection();


        loginButton.setOnAction(event-> {


            String loginEmailText = loginEmail.getText().trim();
            String loginPasswordText = loginPassword.getText().trim();

            User user = new User();
            user.setEmail(loginEmailText);
            user.setPassword(loginPasswordText);

            ResultSet userRow = dbConnection.checkForUser(user);
            int counter = 0;

            try{
                while (userRow.next()){
                    counter++;
                }
                if (counter==1){
    //-------------------Below this line to the next line seems to do nothing-------------------------------------------------------------------

                    FXMLLoader loaderNew = new FXMLLoader(getClass().getResource("/sample/view/Dashboard.fxml"));
                    try {
                        loaderNew.load();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    DashboardController dashboardController = loaderNew.getController();
                    dashboardController.setName(loginEmail.getText().trim());
//---------------------------------------------------------------------------------------

                    loginButton.getScene().getWindow().hide();
                    FXMLLoader loader = new FXMLLoader();
                    loader.setLocation(getClass().getResource("/sample/view/Dashboard.fxml"));

                    try {
                        loader.load();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    Parent root = loader.getRoot();
                    Stage stage = new Stage();
                    stage.setScene(new Scene(root));
                    stage.showAndWait();

                }else{
                    errorLabel.setText("Email or Password Incorrect");
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        });

    }

}

Dashboard controller:

package sample.controller;

//imports here same as above

public class DashboardController {

    @FXML
    private Label nameLbl;


    public void setName(String name){
        nameLbl.setText(name);
    }


    @FXML
    void initialize() {

    }
}

It seems like my method in Dashboard controller is never being called, why is that? Thanks in advance for any help.

i am bad at coding
  • 555
  • 1
  • 3
  • 14

1 Answers1

0

I updated the small segment such that it's only loading one FXML Loader thing and it works now.

                loginButton.getScene().getWindow().hide();
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/sample/view/Dashboard.fxml"));

                try {
                    loader.load();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Parent root = loader.getRoot();
                DashboardController dashboardController = loader.getController();
                dashboardController.setName(loginEmail.getText().trim());
                Stage stage = new Stage();
                stage.setScene(new Scene(root));
                stage.show();
i am bad at coding
  • 555
  • 1
  • 3
  • 14