TableView doesn't commit values on focus lost event

后端 未结 4 1347
不思量自难忘°
不思量自难忘° 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:17

    I had found a simple solution which works in my case for TableCells. The idea is to forget about commitEdit at focus lost. Let javafx do its work, and then just update the value of the previously edited cell.

    abstract class EditingTextCell extends TableCell {
        protected TextField textField;
        private T editedItem;
    
        @Override
        public void startEdit() {
            ...
            textField.focusedProperty().addListener((t, oldval, newval) -> {
                if (!newval) {
                    setItemValue(editedItem, textField.getText());
                }
            });
    
            editedItem = (T) getTableRow().getItem();
        }
        public abstract void setItemValue(T item, String text);
        ...
    }
    

    so, the only trick is to implement the setItemValue() in such a way that it updates the correct part of the item.

提交回复
热议问题