JavaFX8 - How to remove a TableCell which has a tooltip?

163 views Asked by At

I want to remove TableCells out of my TableView. Therefore I call this line:

table.getItems().remove(item);

The TableCell is removed correctly, but the ToolTip will be shown on the next TableCell. For example: When I remove the first Cell, the ToolTip will be shown on the second Cell (which is now the first). How do I avoid this issue?

My CellFactory looks like that:

    column.setCellFactory(c -> {
    TableCell<DataItem, DataItem> cell = new TableCell<DataItem, DataItem>() {
        @Override
        protected void updateItem(DataItem item, boolean empty) {
            super.updateItem(item, empty);
            if (item != null) {
                setText(item.getName());
                if (item.getDescription() != null && !item.getDescription().isEmpty()) {
                    setTooltip(new Tooltip(item.getDescription()));
                }
            }
        }
    };
    return cell;
});
1

There are 1 answers

0
James_D On BEST ANSWER

Handle the case where item is null. If you think in terms of "removing data from the table model", instead of "removing cells" (you're not removing any cells at all, you're just changing the data displayed by the existing cells), this should make sense.

column.setCellFactory(c -> {
    TableCell<DataItem, DataItem> cell = new TableCell<DataItem, DataItem>() {
        @Override
        protected void updateItem(DataItem item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null) {
                setText(null);
                setTooltip(null);
            } else {
                setText(item.getName());
                if (item.getDescription() != null && !item.getDescription().isEmpty()) {
                    setTooltip(new Tooltip(item.getDescription()));
                } else {
                    // may need something here, depending on your application logic...
                }
            }
        }
    };
    return cell;
});