Jay
Jay

Reputation: 13

Store user input in second class from JFrame

What my program is suppose to do is when the user logs into the system, the username which they have entered should be stored in a second class. The reason for this is the username will be used to display at the top right corner of every JFrame which is opened.

So all I want to know is how do I store the username in a class (Named userLogin.java) which is in the background, so when the user opens for example inventory JFrame the username from the userlogin will be displayed in a textbox, then if they proceed to for example salesList JFrame, the username will be called from the userlogin class and displayed in a textbox on this JFrame.

Here is my coding for userlogin.java

public class userlogin{
    //declare user strig for logged on user
    public String users;
    //default contructor
    public LoggedonUser()
    {

    }

   public void setUser(String loggeduser)
   {
       users = loggeduser;
   }
}

How do I alter this class in order to store and hold user input for calling from any JFrame throughout the system use.

Much appreciated for the help, to get this to work :)

Upvotes: 0

Views: 187

Answers (2)

Anptk
Anptk

Reputation: 1123

You can also use static variable for this

create a static variable

public static String USERNAME="";

and when you get username from user, you can,

USERNAME=txt_username.getText();

Then you can use in any class in the project as

userLogin.USERNAME;

NB:-clear the static value at the time of LOGOUT.

Upvotes: 0

tenten
tenten

Reputation: 1276

Hope this code help you;

In the login page add following method; note : usernametxt is the text field and It is public

public void showUsername(){
    String uname = usernametxt.getText();

}

In other Frames ;

note : username_lbl is the Label that get the user name

public void getUsername(String user){
    username_lbl.setText("Logged in as : "+user);
}

Upvotes: 1

Related Questions