我有一个 TableView,它有几个可编辑的列。在用于编辑提交的可编辑表列的 JavaFX Scene Builder 中,我映射了一个 FXML 控制器方法,该方法调用 DAO 服务以从数据库返回数据。问题是在编辑表格单元格后未调用事件处理程序方法。我希望在编辑单元格数据后按 Tab 键时触发此事件。这个怎么做?请建议
3 回答
我对 CheckBoxTableCell 和 DatePickerTableCell 和 ColorPickerTableCells 有同样的问题 :-(
我是这样处理的:在控件的事件上,我取回“ ((Inputs)getTableView().getItems().get(getTableRow().getIndex() ”使用的 POJO 对象,并且我更新类似是否在 OnEditCommit 方法中完成...
所以对我来说它看起来像这样(更新颜色):
((Inputs) getTableView().getItems().get(
getTableRow().getIndex())
).setColor(cp.getValue());
这是 ColorPickerCell 的示例:
public class ColorPickerTableCell<Inputs> extends TableCell<Inputs, Color>{
private ColorPicker cp;
public ColorPickerTableCell(){
cp = new ColorPicker();
cp.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
commitEdit(cp.getValue());
updateItem(cp.getValue(), isEmpty());
((Inputs) getTableView().getItems().get(
getTableRow().getIndex())
).setColor(cp.getValue());
}
});
setGraphic(cp);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setEditable(true);
}
@Override
protected void updateItem(Color item, boolean empty) {
super.updateItem(item, empty);
cp.setVisible(!empty);
this.setItem(item);
cp.setValue(item);
}
}
使用这个简单的 JavaFX 的 POJO:
public ObjectProperty<Color> color = new SimpleObjectProperty<Color>();
this.color = new SimpleObjectProperty(color);
public ObjectProperty<Color> colorProperty() {
return color;
}
public void setColor(Color color2) {
color.set(color2);
}
我不知道这是否是实现这一目标的好方法,但它对我有用......请注意,JavaFX 的 POJO 只能在“ActionEvent”请求(组合框、日期选择器、颜色选择器等)中访问。
问候,
这是我用来从表格视图的可编辑单元格调用我的 DAO 的方法。
private TableColumn<Person, String> createNameCol(){
TableColumn col = new TableColumn("Name");
col.setCellValueFactory(
new PropertyValueFactory<Person, String>("name"));
col.setCellFactory(TextFieldTableCell.forTableColumn());
col.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<Person, String> t) {
Person p = t.getRowValue();
p.setName(t.getNewValue());
sl.update(p); // This is where I call the update method from my DAO.
}
});
return col;
}
如果这不起作用,请发布您的代码。
编辑:
这是可编辑的tableViews的一个很好的教程
在这上面浪费了一天中最好的时间,在互联网上搜索所有说几乎相同的事情并且都在做我已经在做的事情的例子,这就是我发现的:
在 TextFieldTableCell 单元格中编辑值时,您必须按 Enter 键才能进行编辑提交。如果您从单元格中跳出,则单元格仍处于编辑模式(您可以继续按 Tab 键以返回单元格的文本字段,如果您只是单击远离单元格,则调用 TextFieldTableCell 的 cancelEdit 方法从而取消编辑:-(