如何获得对FXML控制器的引用? [英] How can I get a reference to the controller of an FXML?
问题描述
我正在尝试使用JavaFX和Gluon的Scene Builder创建一个小的先登录然后进入主菜单"表单.到目前为止,我已经制作了2个场景,第一个是登录"屏幕,在该屏幕中,我连接了SQLite数据库,在输入正确的用户名和密码后,它可以很好地加载,并切换到第二个场景.对于每个场景,我使用不同的类(FXML/FXML控制器).在第二个场景中,我需要2个标签,这些标签用于根据数据库的数据(更确切地说是First_Name和Role)进行更改.这是我在第一个场景中按下确定"按钮并加载数据库时使用的代码:
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);
您也可以看到,但是它会引发java.lang.reflect.InvocationTargetException错误.
like you can see as well but it throws a java.lang.reflect.InvocationTargetException error.
推荐答案
加载第二个场景时,请获取对控制器的引用,并使用它:
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);
这篇关于如何获得对FXML控制器的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!