根据另一个 tableCell (ComboBox) JavaFx 中的值编辑 TableCell 的内容
我有一个 TableView(如下),我希望用户从 ComboBox 中选择一个项目,然后使用数据库中的值填充接下来的两个单元格(描述和项目成本)。尽管进行了大量的谷歌搜索和头脑风暴,我还没有弄清楚如何访问特定单元格来更改它们的值/文本。当然,invoiceClassDescription.setText() 只是更改列标题,这不是我想要的。
我尝试过 setCellSelectionEnabled(true) 和 invoiceTable.getSelectionModel().getSelectedCells(); 来获取组合框单元格的索引,并可能推断接下来两个单元格的索引就这样,但一切很快就变得非常复杂。
这是 ClassID ComboBox 的代码:
invoiceClassID.setCellValueFactory(new PropertyValueFactory<>("courseID"));
invoiceClassID.setCellFactory(column -> new TableCell<InvoiceItem, ObservableList<String>>(){
private final ComboBox<String> combo = new ComboBox<>(courseIDs);
{
combo.valueProperty().addListener((obs, oldValue, newValue) -> {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection (DB_Updater.DB_URL,DB_Updater.USER,DB_Updater.PASS);
String sql = ("SELECT ClassName, TotalCost from Classes where CourseID = " + '"' + newValue + '"');
ResultSet result = conn.createStatement().executeQuery(sql);
while(result.next()) {
// set class description and cost to values in DB
}
conn.close();
}
catch (Exception e) {
System.out.println(e);
}
});
}
@Override
protected void updateItem(ObservableList<String> items, boolean empty) {
super.updateItem(courseIDs, empty);
if(empty) {
setGraphic(null);
} else {
combo.setItems(courseIDs);
setGraphic(combo);
}
}
});
我想我的问题是,如何访问特定单元格来编辑其内容?
I have a TableView (below) where I would like the user to select an item from a ComboBox which then populates the next two cells (Description and item cost) with values from a Database. In spite of much googling and headbanging, I have not figured out how to access the specific cells to change their values/text. Of Course invoiceClassDescription.setText() just changes the column heading which is not what I want.
I have tried setCellSelectionEnabled(true)
and invoiceTable.getSelectionModel().getSelectedCells();
to then get index of the combobox cell and perhaps infer the index of the next two cells that way but it all got very convoluted very quickly.
Here is the code for the ClassID ComboBox:
invoiceClassID.setCellValueFactory(new PropertyValueFactory<>("courseID"));
invoiceClassID.setCellFactory(column -> new TableCell<InvoiceItem, ObservableList<String>>(){
private final ComboBox<String> combo = new ComboBox<>(courseIDs);
{
combo.valueProperty().addListener((obs, oldValue, newValue) -> {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection (DB_Updater.DB_URL,DB_Updater.USER,DB_Updater.PASS);
String sql = ("SELECT ClassName, TotalCost from Classes where CourseID = " + '"' + newValue + '"');
ResultSet result = conn.createStatement().executeQuery(sql);
while(result.next()) {
// set class description and cost to values in DB
}
conn.close();
}
catch (Exception e) {
System.out.println(e);
}
});
}
@Override
protected void updateItem(ObservableList<String> items, boolean empty) {
super.updateItem(courseIDs, empty);
if(empty) {
setGraphic(null);
} else {
combo.setItems(courseIDs);
setGraphic(combo);
}
}
});
I guess my question is, how does one gain access to a specific cell to edit its contents?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论