-1

I have a LoginGUI and a MainGUI, what I want to do is return the current user as a String or the UserID as an Integer in my MainGUI. My program runs the LoginGUI and then if the UserName a password match up with the database it lets you through to the MainGUI. I've declared a CurrUserID and CurrUser in my LoginGUI - both are public, but how can I use them in my MainGUI?

Here is the code for my LoginGUI - I'm using swing.

    private void loginButActionPerformed(java.awt.event.ActionEvent evt) {                                         
    for (int i = 0; i <= size; i++) {
        if (login.getText().equals(users[i].getUser())) {
            currUser = users[i].getUserID();
            if (password.getText().equals(users[i].getPassword())) {
                try {
                    MainGUI main = new MainGUI(users);
                    main.setVisible(true);
                    this.dispose();
                } catch (SQLException ex) {
                    Logger.getLogger(LoginGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else{
                JOptionPane.showMessageDialog(null, "Incorrect Password");
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Incorrect UserName");
        }
    }
}                                        

public int getCurrUser() {
    return currUser;
}

What I want to do is return the currUser while I'm in my MainGUI. How can I?

3
  • 2
    Post related code. It is tough to suggest anything without seeing what you have.
    – kosa
    Sep 17, 2012 at 14:47
  • 1
    You are going to have to give us a little more here. Are you using Swing? A framework? Sep 17, 2012 at 14:47
  • what do you mean by "My program runs the LoginGUI" are you instantiating an instance? running a process? and how does MainGUI get started?
    – JTMon
    Sep 17, 2012 at 14:48

3 Answers 3

1

Add a member, say String uid; to your MainGUI class, and create two constructors, one that accepts String and other that accepts Integer; in 'em, set uuid to whatever is passed. Now you'll have your user id tied to MainGUI for later use.

So, when you're done with LoginGUI, initialize the new MainGUI(passedID).

1

You could add a setter method in your MainGUI class and call it like so:

main.setCurrentUser(currUser);

For passing multiple fields from LoginGUI to MainGUI you could use a wrapper class, e.g.

CurrUserDetails details = new CurrUserDetails(userID, userName);
main.setCurrentUserDetails(currUser);
0

in order to invoke any non-static method of a class, you require an instance of the class, for instance

public class Foo{
    public void bar(){
        // do stuff
    }
}

Foo f = new Foo();
f.bar(); // invoking non-static method of class Foo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.