Skip to content
Advertisement

Get Current logged User Id and User Name using JavaFX

I’m trying to create an application using JavaFX. I want to get current logged userid and username, after successful login. I want to display it is in the home page. How can i do this? please help

MediaController.java

    @FXML
    private Label tf_getname;

    @FXML
    void happyButton(ActionEvent event) {

        DbConnect dbconnect=new DbConnect();
        Connection conn=dbconnect.getConnection();

        String username = tf_getname.getText();

//        String source1 = event.getSource().toString(); //yields complete string
        //String source2 = event.getPickResult().getIntersectedNode().getId(); //returns JUST the id of the object that was clicked
//        System.out.println("Full String: " + source1);
//        System.out.println("Just the id: " + source2);
//        System.out.println(" " + source2);

        try {

            String sql = "SELECT name FROM users WHERE name='"+username+"'";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                tf_getname.setText(rs.getString("name"));

            }


        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

    }

Advertisement

Answer

Let me get this straight, you have the user login, then change the scene to a main window, but you want to remember the user that logged in and display that username on the homepage?

Sounds like you will have to pass data between scenes.

For that you will need to approach this in OOP. Have a object class representing your user with all the getters and setters.

public class User {

    private String email;
    

    public User(String email) {
        this.email = email;
    
    }

    public String getEmail() {
        return email;
    }

}

When you connect to the database at login, validate user then instantiate an object of the “User” class for example, then pass it to the mainwindow scene your are loading.

public class LoginController implements Initializable {
    public User user;


    // All your FXML code



  @FXML
void handleLogin(ActionEvent actionEvent) throws IOException {
    // Do your validation and then call the changeToMainWindow()
    changeToMainWindow();
}

}

Have a “initData” class or something in the mainwindowcontroller.

Like

public void initData(User user) {
        selectedUser = user;
        labelUser.setText(selectedUser.getEmail());
    }

Then from your login class, upon validation, send the data to the mainwindow before changing your scene by instantiating your User, then passing the object to the initData method from your second scene.

//User validation, then:
// Get the FXMLLoader then
//Instantiate the mainwindow 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);

        // access the controller
        MainWindowController mainWindowController = loader.getController();
        mainWindowController.initData(user);

        Stage primaryStage = (Stage) loginButton.getScene().getWindow();
        primaryStage.setScene(mainScene);
        primaryStage.show();
    }

Then upoin login, use the changeToMainWindow() method and it’ll pass the user.

In the above example I am simply passing email, but you get the point.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement