-1

I have a login stage and then home stage, there are many actions I want to do for each user, so I want the userID for the current user. How do I parse the Id or the username from login stage to home stage ? Note that there is a public database connected and when the user enter his username and password, the system will check if the data are true and exist in the database. This is my login stage controller after checking the entered data:

Stage HomeScreen = new Stage();
Parent root=null;

                        try {

root=FXMLLoader.load(getClass().getResource("Home.fxml"));

                        }
                        catch (IOException ex) {

Logger.getLogger(LoginScreen.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        Stage current = 
(Stage)username.getScene().getWindow();
                        Scene scene = new Scene(root);
                        HomeScreen.setScene(scene);
                        HomeScreen.initStyle(StageStyle.DECORATED);
                        HomeScreen.setTitle("Network Automation");
                        current.hide();
                        HomeScreen.show();
  • 1
    This may help: https://stackoverflow.com/questions/22178533/communication-between-two-javafx-controllers –  Jan 21 '19 at 23:27
  • Too long and far about what I need :/. I just need to know what is the concept and the way to do that @Vajk – Mohammad Qabaha Jan 21 '19 at 23:31
  • 2
    This exact question has been asked and answered dozens of times already, @MohammadQabaha. Sometimes reading the longer answers will be the best way to learn... – Zephyr Jan 22 '19 at 01:12
  • unrelated to your problem: please learn java naming conventions and stick to them – kleopatra Jan 22 '19 at 11:35

1 Answers1

0

I assume this can be done in multiple ways. You can set the user details in static variable to access across the application. Or a more reasonable approach would be to pass the details to the Home page controller.

Lets say the controller for Home.fxml is HomeController.java and there is a setter/getter for userId in it. Then your code needs to be changed as below.

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("Home.fxml"));
Parent root = myLoader.load();
HomeController homeController = (HomeController) myLoader.getController();
homeController.setUserId(userId); // set the userId in the controller...
// do the other stuff as usual...
Sai Dandem
  • 8,229
  • 11
  • 26
  • I sis that but when I tried to print the userID in the initialize method it was null. @SaiDandem – Mohammad Qabaha Jan 22 '19 at 19:05
  • If you are saying about the initialize method of HomeController, then yes you will not get userId in initialize method. Because you cannot set the userId when initializing it (i.e, myLoader.load()). – Sai Dandem Jan 22 '19 at 23:10