-1

How can I pass variables between two controllers that are initializable? After logging in, the username is null in the second page because it already initialized(I am using a framework). Can I run a method to load the variables after initialization?

So here is a code snippet: logincontroller

public static String myusername;
public static String myaccount; 

@FXML
private JFXTextField username;

@FXML
public void makeLogin(ActionEvent event) {      
Document query = new Document();
query.put("userName", username.getText());

FindIterable<Document> docs = userCollection.find(query);

if(docs != null){
    if(detailsmatch(username, password)){

        String theaccount = (String) doc.get("account");

        USERSingleton.getInstance().setusername(username.getText());                                
        USERSingleton.getInstance().setaccount(theaccount);

        myusername = USERSingleton.getInstance().getusername();
        myaccount = USERSingleton.getInstance().getaccount();

        myScreenPane.setHomeScreen("home");

    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {     

}  

@Override
public void setScreenPane(screenPane screenPage) {
    myScreenPane = screenPage;
}
}

So I would like to get the account variable in the homecontroller from the logincontroller. Like:

homecotroller

System.out.println(logincontroller.myaccount);
System.out.println(USERSingleton.getInstance().getaccount());

. .

DennisKip
  • 19
  • 6

1 Answers1

0

You can use a singleton for this.

Create a singleton and pass all variables via setters to the instance. Then you can access the object from your other page.

  • Thanx.. Now I have a singleton but how do I access the username from the the singleton for the different users? – DennisKip Sep 12 '18 at 08:06
  • You need to provide more information. From your response I asume that you have a system where you need to access multiple users from your client. Where do you get the data from? From inputfields? From the network? In general just store the reference to the object you read the data from (whatever this is) – LeBraveLittleToaster Sep 12 '18 at 08:10
  • Ok, so it is a desktop app. a user logs in.. a singleton is created after the details match those that are in the database(username and password).. the homescreen then loads. So how will I differentiate the different singleton instances of the different users? – DennisKip Sep 12 '18 at 08:18
  • Maybe I get you wrong but normally you have only one user per JavaFX instance. Cause each instance only has one singleton instance you just store the username/pw/id/etc. in the singleton and access in your homescreen. Or do you have multiple login users per homescreen? – LeBraveLittleToaster Sep 12 '18 at 08:22
  • ooh ok. No actually just one. so, USERSingleton.getInstance().setusername(theusername); that's how i set the singleton.. Now how do I read it from the homescreen after initialization? (username = USERSingleton.getInstance().getusername();) does work. – DennisKip Sep 12 '18 at 08:27
  • btw. care that your singleton (or any other construct your share between threads) is threadsafe otherwise you can run into issues cause javafx normally uses a GUI thread and want you to do anything else in other threads (so that visual thread is not block ergo GUI not freezes) – LeBraveLittleToaster Sep 12 '18 at 08:30
  • sorry, I meant (username = USERSingleton.getInstance().getusername();) does NOT work. – DennisKip Sep 12 '18 at 08:40
  • The line itself works. Can you provide some code? Otherwise it’s hard to tell. You told us that the second initialize is before you feed the Data to the singelton. In thats true, them you can do either active waiting (boolean isDataAvaible) but if the initialize have to run through, then you have to Call a method (that loses the Data from the singleton) when the First (loginwindow) closes. But it s Hard to Tell without actual code – LeBraveLittleToaster Sep 12 '18 at 09:22
  • Edited the question with some code. – DennisKip Sep 12 '18 at 11:08