TableView doesn't commit values on focus lost event

后端 未结 4 1348
不思量自难忘°
不思量自难忘° 2020-11-29 09:41

I\'d like to create a table with the following features:

  • Edit on key press
  • Enter key = next row
  • Tab key = next column
  • Escape key = c
4条回答
  •  误落风尘
    2020-11-29 10:01

    I got curious and did some background research.

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

    Background

    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
    }
    

    Why does it return 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.

    Solutions / 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.

    • TableView, TreeView, ListView - Clicking outside of the edited cell, node, or entry should commit the value
    • TableCell - commit on focus lost not possible in every case

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

提交回复
热议问题