如何在焦点上启用提交对于 TableView/TreeTableView 丢失?
有没有简单的方法可以让 TreeTableView(或 TableView)尝试在焦点丢失时提交值?
不幸的是,我没有成功使用javafx TableCellFactories的任何默认实现,这就是为什么我尝试了自己的TreeTableCell实现以及一些不同的tableCell实现,例如Graham Smith的实现,这似乎是最直接的,因为它已经实现了焦点丢失的钩子,但是该值从未提交并且用户更改被重置为原始值。
我的猜测是,每当焦点丢失时,受影响单元格的编辑属性总是已经是错误的,这导致单元格永远不会在focusLost上提交值。这里是原始(oracle-)TreeTableCell实现(8u20ea)的相关部分,这导致我的方法失败:
@Override public void commitEdit(T newValue) {
if (! isEditing()) return; // <-- here my approaches are blocked, because on focus lost its not editing anymore.
final TreeTableView<S> table = getTreeTableView();
if (table != null) {
@SuppressWarnings("unchecked")
TreeTablePosition<S,T> editingCell = (TreeTablePosition<S,T>) table.getEditingCell();
// Inform the TableView of the edit being ready to be committed.
CellEditEvent<S,T> editEvent = new CellEditEvent<S,T>(
table,
editingCell,
TreeTableColumn.<S,T>editCommitEvent(),
newValue
);
Event.fireEvent(getTableColumn(), editEvent);
}
// inform parent classes of the commit, so that they can switch us
// out of the editing state.
// This MUST come before the updateItem call below, otherwise it will
// call cancelEdit(), resulting in both commit and cancel events being
// fired (as identified in RT-29650)
super.commitEdit(newValue);
// update the item within this cell, so that it represents the new value
updateItem(newValue, false);
if (table != null) {
// reset the editing cell on the TableView
table.edit(-1, null);
// request focus back onto the table, only if the current focus
// owner has the table as a parent (otherwise the user might have
// clicked out of the table entirely and given focus to something else.
// It would be rude of us to request it back again.
ControlUtils.requestFocusOnControlOnlyIfCurrentFocusOwnerIsChild(table);
}
}
在调用原始 commitEdit() 方法之前,我成功地重写了此方法并“手动”提交值,但这会导致对键(如 enter)的提交提交值两次(在键上 +焦点丢失)。此外,我一点也不喜欢我的方法,所以我想知道,是否有其他人以“更好”的方式解决了这个问题?