简体   繁体   中英

sending from jframe to java class and then accessing them from other jframe

So let's say I have 3 files.

  • Accounts.java (Account class)

  • loginGUI.java (JForm)

  • displayAccountGUI.java (JForm)

when I login I input CustomerID, Pin and Account Number. Then the program checks if the 3 values I have inputted are in accounts.txt.

I also have Account a = new Account(); in both of the Jframes.

In displayAccount.java I want to display Account Number from loginGUI.java that is being stored in Account.java using a.setId(ID); but because I have Account a = new Account(); in displayAccount.java the value set before is being removed due to creation new 'Account'.

Is there any way I can access this value in some other way?

Once you've validated the user credentials, create a "session" object, which encapsulates the information you need to share. It could be the "customer ID" and "account name" information or it could be an instance of the Account class.

Pass this to your displayAccount class (removing the Account a = new Account(); and using the passed reference)

How you achieve all this will depend on how you've structured your code. Me personally, I'd have some kind of controller which displayed a login dialog. When the dialog is dismissed, I'd validate the credentials, displaying an error message if they failed. If they succeeded, I'd create a new instance of the Account class (assuming it holds enough information about the client), I'd create the "account view", passing the instance of Account to it

This is a relatively basic programming concept, consider having a look at Passing Information to a Method or a Constructor

I belive you can create a new constructor for displayAccountGUI and pass Account as a parameter. It makes sense because in the future you may need more information from the account in displayAccountGUI , like a photo or something else.

Since I don't have the privilege to add comments yet, I'll just post it here. I only know two of many ways to accomplish what you want to do to get the desired result.

  1. pass the value via a constructor

    public displayAccount(Account a){this.a = a;}

  2. use setter method

    public void setAccount(Account a){this.a = a;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM