且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

TableView 不会在焦点丢失事件上提交值

更新时间:2021-09-09 09:54:48

我很好奇,做了一些背景调查.

您正面临 JavaFX 中一个众所周知的错误的问题.

You are facing the problem of a well-known bug in the JavaFX.

当你调用 commitEdit(textField.getText()) 时,它做的第一件事就是检查 isEditing() 的值,如果值为 false,不提交.

When you call commitEdit(textField.getText()), the first thing it does is to check the value of isEditing() and returns if the value is false, without committing.

public void commitEdit(T newValue) {
    if (! isEditing()) return;

    ... // Rest of the things
}

为什么返回false?

您可能已经发现,只要您按下 TABENTER 来更改您的选择,cancelEdit() 就会被调用将 TableCell.isEditing() 设置为 false.当 textField 的焦点属性侦听器中的 commitEdit() 被调用时,isEditing() 已经返回 false.

As you have probably found out, as soon as you press TAB or ENTER to change your selection, cancelEdit() is called which sets the TableCell.isEditing() to false. By the time the commitEdit() inside textField's focus property listener is called, isEditing() is already returning false.

JavaFX 社区一直在讨论该主题.那里的人已经发布了hacks,欢迎您查看.

There have been on going discussion on the Topic in JavaFX community. People in there have posted hacks, which you are most welcome to look at.

一个 SO 线程 中显示了一个 hack,它似乎完成了工作,尽管我还没有试过了(还).

There is a hack shown in a SO thread, which seems to get the job done, although I haven't tried it (yet).