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

对着背影说爱祢 提交于 2021-01-28 09:08:05

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/61520854/how-can-i-get-a-reference-to-the-controller-of-an-fxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!