[英]How do I restore a previous value in JavaFX TableColumn's onEditCommit?
I have a table written in JavaFX, that the user has the option to edit one of its columns.我有一个用 JavaFX 编写的表,用户可以选择编辑其中一个列。 When an editing of a cell takes place, I try to rename some internal object based on the new text.当对单元格进行编辑时,我尝试根据新文本重命名一些内部对象。 If that renaming fails, I issue a notification + popup an error dialog, and then I'd like to restore the original text into that field.如果重命名失败,我会发出通知+弹出错误对话框,然后我想将原始文本恢复到该字段中。 For some reason, this gets me into an infinite loop of error popups.出于某种原因,这让我陷入了错误弹出的无限循环。
The code I currently have from the original writer is as follows:我目前从原作者那里得到的代码如下:
mNameColumn.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<StateDefinition, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<StateDefinition, String> event) {
try {
someObject.rename(((StateDefinition) event.getTableView().getItems().get(event.getTablePosition().getRow())).getState(), event.getNewValue());
} catch (MYException ex) {
MyNotificationCollector.addNotification("Failed renaming: " + ex.getLocalizedMessage(), NotificationType.SYSTEM_WARNING);
//popup error message code here
// Need to revert the cell's text to the previous value here...
}
}
});
Try to use 尝试使用
TableColumn.CellEditEvent.getOldValue()
For example: 例如:
try {
someObject.rename(((StateDefinition) event.getTableView().getItems()
.get(event.getTablePosition().getRow())).getState(),
event.getNewValue());
} catch (MYException ex) {
MyNotificationCollector.addNotification("Failed renaming: "
+ ex.getLocalizedMessage(), NotificationType.SYSTEM_WARNING);
// popup error message code here
// Revert the cell's text to the previous value here...
someObject.rename(((StateDefinition) event.getTableView().getItems()
.get(event.getTablePosition().getRow())).getState(),
event.getOldValue());
// workaround for refreshing rendered view
event.getTableView().getColumns().get(0).setVisible(false);
event.getTableView().getColumns().get(0).setVisible(true);
}
The last 2 lines are oldy workarounds for refreshing tableview rendered values. 最后两行是用于刷新tableview呈现值的旧方法。
EDIT: 编辑:
If the backed data model (item fields of tableview) is not changed when the exception is thrown then just update tableview rendering in catch block with: 如果抛出异常时未更改支持的数据模型(tableview的项目字段),则只需使用以下命令更新catch块中的tableview呈现:
} catch (MYException ex) {
MyNotificationCollector.addNotification("Failed renaming: "
+ ex.getLocalizedMessage(), NotificationType.SYSTEM_WARNING);
// workaround for refreshing rendered view
event.getTableView().getColumns().get(0).setVisible(false);
event.getTableView().getColumns().get(0).setVisible(true);
}
It's a bug in TableCell<\/a> (actually, all cells - though not noticeable for all types due to different reasons): happens if a custom commit handler rejects the edited value (or changes it in any way before writing it back to the data).这是TableCell 中的一个错误<\/a>(实际上,所有单元格 - 尽管由于不同的原因,所有类型的单元格都不明显):如果自定义提交处理程序拒绝编辑的值(或在将其写回数据之前以任何方式更改它),则会发生这种情况。 The technical reason is calling updateItem with the newValue技术原因是用 newValue 调用 updateItem
@Override
public void commitEdit(T newValue) {
// ... cell book-keeping
// ... fire editCommitEvent to allow handler to save value
// update the item within this cell, so that it represents the new value
updateItem(newValue, false); // <-- assumes that the handler saves the new value as-is
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.