Search code examples
javajavafxjavafx-8treetableview

JavaFX TreeTableView column setOnEditCommit() refuses to accept handler to save edited cell


In a project of mine, I'm using a TreeTableView from JavaFX to display some objects of type BillingTableRow. I have implemented a cell editor, but when I try to set the onEditCommit event, I am unable to get the content types to match.

Here is my code for one of the columns storing names of type String inside the object of type BillingTableRow:

    // Name column
    Callback<TreeTableColumn<BillingTableRow, String>, TreeTableCell<BillingTableRow, String>> nameCallback = new Callback<TreeTableColumn<BillingTableRow, String>, TreeTableCell<BillingTableRow, String>>() {
        @Override
        public TreeTableCell<BillingTableRow, String> call(TreeTableColumn<BillingTableRow, String> p) {
            return new TextFieldTreeTableCell<BillingTableRow, String>();
        }
    };
    nameColumn.setCellFactory(nameCallback);
    nameColumn.setOnEditCommit(new EventHandler<CellEditEvent<BillingTableRow, String>>() {
        @Override
        public void handle(CellEditEvent<BillingTableRow, String> t) {
            ((BillingTableRow) t.getTableView().getItems().get(t.getTablePosition().getRow())).setName(t.getNewValue());
        }
    });

Can someone please tell me what is wrong? I get the following error in Eclipse:

The method setOnEditCommit(EventHandler< TreeTableColumn.CellEditEvent>) in the type TreeTableColumn is not applicable for the arguments (new EventHandler< TableColumn.CellEditEvent< BillingTableRow,String>>(){})

I appreciate any help with my problem.


Solution

  • That is because you have an import

    import javafx.scene.control.TableColumn.CellEditEvent;
    

    So when you define

    EventHandler<CellEditEvent<BillingTableRow, String>>
    

    the compiler gets this CellEditEvent as TableColumn.CellEditEvent and not TreeTableColumn.CellEditEvent, and gives the error.

    To fix it, delete that import and write expilcitly

    EventHandler<TreeTableColumn.CellEditEvent<BillingTableRow, String>>