我正在用 JavaFX 编写可编辑表我想验证输入(例如,表/列中是否已经存在相同的值)如果验证失败我想拒绝新值。换句话说,当用户在单元格中键入新值并按 ENTER 时,我会进行验证,如果失败,我想在单元格中查看旧值。
我的代码看起来与此示例非常相似 http://java-buddy.blogspot.com/2012/04/javafx-2-editable-tableview.html
我以为我可以做这样的事情
colName.setCellFactory(trainerStringCellFactory);
colName.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<GymTrainer, String>>() {
public void handle(TableColumn.CellEditEvent<GymTrainer, String> t) {
GymTrainer newTrainer = new GymTrainer(t.getNewValue(), trainer.getSurrname(), trainer.getRateOfPay());
newTrainer.setTrainerId(trainer.getTrainerId());
if ( !updateTrainer(newTrainer) ) {
// Here I would like to replace value in editable cell
}
}
}
});
有谁知道如何做到这一点,或者这样做的地方不对?