javajavafxcontrollerfxmlfxmlloader

How can I get a reference to the controller of an FXML?


I'm trying to make a small "Login then Main Menu" form using JavaFX and Gluon's Scene Builder. I've made 2 scenes so far, the first one is a "Login" screen in which I've connected a SQLite Database, after putting the right Username and Password it loads perfectly fine and it changes to the second scene. For each scene I use a different class (FXML / FXML Controller). In the second scene I want 2 labels that I use to change according to the Database's data (More specifically First_Name and Role). This is the code I use when I press the "OK" Button in the first scene and it loads the Database:

public class FXMLDocumentController implements Initializable {
    public static Connection con;
    public static ResultSet rs;
    public static String Name;
    public static String Role;

    @FXML
    private void OKButtonAction(ActionEvent event) throws SQLException, IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("MainMenuFXML.fxml"));
        MainMenuFXMLController mainmenu = loader.getController();
        try {
            PreparedStatement pst ;
            // db parameters
            String DBlink = "jdbc:sqlite:"+System.getProperty("user.dir")+"//MedExpressDB.db";
            // create a connection to the database
            con = DriverManager.getConnection(DBlink);            
            String sql = "SELECT ID,UserName,Password,First_Name,Last_Name,Role,Address,Town,Phone,AFM,AMKA,Email FROM Users where (UserName = ? and Password = ?)";
            pst = con.prepareStatement(sql);
            pst.setString(1, user.getText());
            pst.setString(2, pass.getText());
            rs = pst.executeQuery();
            if (rs.next() == false){
                System.out.println("Λάθος κωδικός ή Username!");
            }else{
                Name = "WElcome, "+rs.getString("First_Name");
                Role = rs.getString("Role");
                mainmenu.setLabels(Name, Role);
                Parent root = FXMLLoader.load(getClass().getResource("MainMenuFXML.fxml"));
                Scene scene2 = new Scene(root);
                Stage stage = (Stage) okbutton.getScene().getWindow();
                stage.setScene(scene2);
                System.out.println("\nConnection has been established!\nWelcome!");

            } 
        }
         catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }  
    }
}

And in the second class where I use the second scene I have:

public class MainMenuFXMLController implements Initializable {

    @FXML
    private Label welcomelbl;

    @FXML
    private Label rolelbl;

    public void setLabels(String name, String role){
        welcomelbl.setText(name);
        rolelbl.setText(role);
    }
}

//I tried the:
public void setLabels(x,y){
   welcomelbl.setText(x);
   rolelbl.setText(y);
}
//and use setLabels in the 1st Class:
                FXMLLoader loader = new FXMLLoader(getClass().getResource("MainMenuFXML.fxml"));
                MainMenuFXMLController mainmenu = loader.getController();
                Name = "Welcome, "+rs.getString("First_Name");
                Role = rs.getString("Role");
                mainmenu.setLabels(Name, Role);

like you can see as well but it throws a java.lang.reflect.InvocationTargetException error.


Solution

  • When you load the 2nd scene get a reference to the controller, and use it:

    FXMLLoader loader = new FXMLLoader(getClass().getResource("MainMenuFXML.fxml")); //once only, not twice as posted ! 
    Parent root = loader.load();  //this is essential 
    MainMenuFXMLController mainmenu = loader.getController();
    mainmenu.setLabels(Name, Role);