How to commit when clicking outside an editable TableView cell in JavaFX?

后端 未结 4 842
清歌不尽
清歌不尽 2020-12-29 10:25

I have a table cell factory responsible for creating an editable cell in a JavaFX TableView.

I\'m trying to implement some added functionality to the tableview so th

4条回答
  •  星月不相逢
    2020-12-29 10:59

    You could do it by overriding the method commitEdit as next:

    @Override
    public void commitEdit(T item) {
        // This block is necessary to support commit on losing focus, because 
        // the baked-in mechanism sets our editing state to false before we can 
        // intercept the loss of focus. The default commitEdit(...) method 
        // simply bails if we are not editing...
        if (!isEditing() && !item.equals(getItem())) {
            TableView table = getTableView();
            if (table != null) {
                TableColumn column = getTableColumn();
                CellEditEvent event = new CellEditEvent<>(
                    table, new TablePosition(table, getIndex(), column), 
                    TableColumn.editCommitEvent(), item
                );
                Event.fireEvent(column, event);
            }
        }
    
        super.commitEdit(item);
    }
    

    This workaround comes from https://gist.github.com/james-d/be5bbd6255a4640a5357#file-editcell-java-L109

提交回复
热议问题