0

I am working on my project where I have UI where the user has to enter his username and password in the text fields in the login stage, which moves him to the next stage where I want to know which user is currently logged in. Both stages have separate controllers. I tried to make the setter and getter in the login stage where I set the user to the username he enters in the username text field and get it from the next stage.

Simplified login Screen:

private String currentUser;
public void setCurrentUser(String currentUser)
    {
        this.currentUser = currentUser;
    }

    public String getCurrentUser()
    {
        return currentUser;
    }

public void validateLogin() {
        DatabaseConnection connectNow = new DatabaseConnection();
        Connection connectDB = connectNow.getConnection();

        String verifyLogin = "SELECT count(1) FROM table WHERE username = '" + usernameTextField.getText() + "' AND password = '" + enterPasswordField.getText() + "'";
        
         try {
            Statement statement = connectDB.createStatement();
            ResultSet queryResult = statement.executeQuery(verifyLogin);

            while (queryResult.next()) {
                if (queryResult.getInt(1) == 1) {
                    setCurrentUser(usernameTextField.getText());
                    openScreen();
                } else {
                    loginMessageLabel.setText("No such user");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            e.getCause();
        }

When I run this method in the stage I get in after successful login in, it returns null:

System.out.println(userController.getCurrentUser());

Is there any way to make it work this way? Sorry for a poor explanation, hope u get what I meant

  • 1
    I think you need to either pass the data to the next controller or use an MVC approach. I recommend MVC. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 May 16 '21 at 02:04
  • 1
    Can you post the code from where you are calling the `System.out.println(userController.getCurrentUser());` and also where are you assigning the userController? Also, it is a good practice to use add-in your variables by preparing the statement instead of creating it as a String. – bosowski May 15 '21 at 23:45
  • Just pass the username (or an object containing the username, i.e. a model) to the next controller. See https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D May 17 '21 at 15:53

1 Answers1

0

Create a method in your second controller and name it initData or something. If user validation is correct, you can pass the username or email, or even an object to the second controller from your login controller.

First, create the initData() method in your main controller with either a string or object as parameter because you will need to pass something to it. In this example the parameter passed to the method is an object of a User class, but you can pass just the username string if you want. Passing an object gives you a lot more flexibility though.

//In main window controller
    public void initData(User loggedUser) {
    userLoggedIn = loggedUser;
    labelUserLoggedIn.setText(userLoggedIn.getEmail());
}

Then, in your login controller you can do your user validation, then pass the object/string to the second controller before changing to the second window.

Here is an example I currently use in an app. First step is to create an instance of FXMLLoader, then load the mainwindow.fxml, set the scene, get the second controller and then pass the object to the initData() method we created before changing to the second window.

I use this method once user validation is complete to pass the object then change to the second window. There might be better ways to do this, but this works fine.

// In second controller
 public void changeToMainWindow() throws IOException {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("mainwindow.fxml"));
    Parent root = loader.load();
    Scene mainScene = new Scene(root);

    MainWindowController mainWindowController = loader.getController();
    mainWindowController.initData(loggedUser);

    Stage primaryStage = (Stage) loginButton.getScene().getWindow();
    primaryStage.setScene(mainScene);
    primaryStage.show();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
ADSquared
  • 207
  • 3
  • 12