我正在尝试启动一个“编辑客户”窗口,其中的文本字段填充了表格行的引用。表和对话框都有不同的控制器类。
以下是相关表格中的代码段,当用户双击某一行时,它会为我们提供所需的customerID。
表格控制器:CustomersController
:
@Override
public void initialize(URL location, ResourceBundle resources) {
populateCustomerTable();
tableListeners(null);
}
void tableListeners(CustomerData customerData){
tblcustomer.setRowFactory(tr -> {
TableRow<CustomerData> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (!row.isEmpty())) {
int selectedCustomerID = row.getItem().getCustomerID();
System.out.println("A certain row: " + selectedCustomerID + " has been clicked!");
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader();
try {
Parent root = loader.load(getClass().getResource("../view/popups/edit_customer.fxml"));
stage.setScene(new Scene(root));
stage.setTitle("Editing Existing Customer's Details");
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(btnEditCustomer.getScene().getWindow());
stage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
});
return row;
});
}
我希望上面的代码中的selectedCustomerID
被解析为EditCustomerController
类,因此当对话框启动时,它的文本字段应该预先填充从查询数据库的select查询中提供的值来自selectedCustomerID
类的CustomersController
的where条件。
EditCustomerController
类的代码段:
@Override
public void initialize(URL location, ResourceBundle resources) {
//populateEditCustomerFields(1);
}
void populateEditCustomerFields(int customerID){
this.customer_ID=customerID;
System.out.println(customer_ID);
try {
con = DatabaseConnection.getConnected();
stmt = con.createStatement();
rs = con.createStatement().executeQuery("SELECT * FROM `h_customers` WHERE `customerID`=" + customer_ID);
while (rs.next()) {
title.setText(rs.getString("title"));
firstName.setText(rs.getString("firstName"));
lastName.setText(rs.getString("lastName"));
nationalID.setText(String.valueOf(rs.getInt("nationalID")));
//dob.setText(rs.getString("DOB"));
mobilePhone.setText(rs.getString("mobilePhone"));
workPhone.setText(rs.getString("workPhone"));
email.setText(rs.getString("email"));
}
} catch (SQLException ex) {
Logger.getLogger(NewRoomController.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
这里的想法是将selectedCustomerID
从CustomersController
解析为EditCustomerController
的初始化方法,以便Dialog可以启动需要编辑的客户详细信息。我在网上寻找解决方案,在StackOverflow上,有些人接近回答我,有些对我的新手来说过于复杂,但没有人帮助过。任何解决方案将受到高度赞赏。我将提供所需的任何进一步澄清。
答案 0 :(得分:0)
您可以获取控制器类并调用其必要的方法。请参阅this answer for getting controller,然后执行
editCustomerController.populateEditCustomerFields(selectedCustomerID);
在表格行上双击。
为了提高性能,您只需加载edit_customer.fxml
一次,当用户双击时,使用editCustomerController.populateEditCustomerFields(selectedCustomerID)刷新其呈现的数据。