0

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.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153

1 Answers1

1

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>>
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • That works, thank you. My only problem now is that I don't know how to fetch the data object for the current row and invoke the setName() method, passing t.getNewValue() as the new name parameter. – Peter Jonsson Oct 19 '14 at 20:24
  • @uluik no, it fires, but I am unable to get the underlaying data object of the row. I want to fetch it in order to invoke the setName() method on it. I have no idea how to do this. It worked perfectly when I used a table, but with the TreeTableView, things seem to be more complicated. – Peter Jonsson Oct 20 '14 at 11:30
  • @Uluik. Nevermind, I solved it. It turns out that it was actually pretty easy. The following code fetches the `BillingTableRow: BillingTableRow row = t.getTreeTablePosition().getTreeItem().getValue();` I guess that I was just too tired to realize it yesterday. Thank you for the help. – Peter Jonsson Oct 20 '14 at 11:38