-1

I installed a custom CellValueFactory and CellFactory on my TableColumn: I'm programming an address book, Salutation is an enum with values "Mr" and "Mrs".

salutation.setCellValueFactory(c->new ReadOnlyObjectWrapper<>(c.getValue().getSalutation()));
salutation.setCellFactory((tableColumn)->new TableCell());

Unfortunatelly the cells set by my CellFactory don't show anything. (Note: actually I want to use a custom TableCell, I only used TableCell for testing). What is the best way to show the item.toString() String on each cell?

Thanks :)

Thanks

Lukas
  • 381
  • 3
  • 13

1 Answers1

1

Note: actually I want to use a custom TableCell, I only used TableCell for testing

This is exactly the issue. TableCell in contrast to the cell type produced by the default cell factory simply updates the empty property of the cell. Removing this initialisation or replacing the assignment of the factory with

salutation.setCellFactory(TableColumn.DEFAULT_CELL_FACTORY);

would show the result of invoking toString on the cell item as would the following cellFactory:

salutation.setCellFactory((tableColumn)->new TableCell() {

    @Override
    protected void updateItem(Object item, boolean empty) {
        super.updateItem(item, empty);
        setText(item == null ? "" : item.toString());
    }

});

Note: I recommend not using the raw type but lacking the knowledge about the types involved, I decided to keep using the raw type in the above snippet.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • So is this how the default CellFactory is implemented? – Lukas Mar 14 '19 at 15:07
  • Pretty much the way I show in the answer, but it also considers the case where the item is a `Node`; in this case it shows the item in the `graphic` property instead. If you take a look at the source code of `TableColumn`, the factory is stored in the `DEFAULT_CELL_FACTORY` field... – fabian Mar 14 '19 at 15:16
  • @Lukas have a look at the source files, then you'll know for certain how the default is implemented :) – kleopatra Mar 14 '19 at 17:07