Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current cell value from cell factory?

I have TableView with 5 TableColumn. One of these columns represent color of culture on the map.

    colorColumn.setCellValueFactory(cellData -> {
        return new SimpleObjectProperty<Culture>(cellData.getValue());
        });
    colorColumn
            .setCellFactory(value -> {
                CellShape<Rectangle, Culture> elem = new CellShape<Rectangle, Culture>(
                        new Rectangle(50.0, 30.0, COLOR_OF_APPROPRIATE_CULTURE));
                elem.setAlignment(Pos.CENTER);
                return elem;
            });   

Here COLOR_OF_APPROPRIATE_CULTURE - it is color that is set in Culture object.

  public class Culture
{
    private Color color;
    //setter and getter
}   

So, how to get this color field in CellFactory?

like image 520
Bohdan Z. Avatar asked Aug 22 '15 10:08

Bohdan Z.


1 Answers

I assume, CellShape is a TableCell subclass, since the callback for setCellFactory() must return a TableCell or descendant. If so, you can get hold of the row data object via the getTableRow() method to TableCell. It would look like:

myColumn.setCellFactory(new Callback<TableColumn<Data, Fieldtype>, TableCell<Data, Fieldtype>>() {

  @Override
  public TableCell<Data, Fieldtype> call(TableColumn<Data Fieldtype> param) {
    return new TableCell<Data, Fieldtype>(){

      @Override
      protected void updateItem(Fieldtype item, boolean empty) {
        super.updateItem(item, empty); 

        Object rowDataItem = this.getTableRow().getItem();
        // Do what with the row data what you like ...

        // ...
      }
    };
  }
});
like image 146
Robert Rohm Avatar answered Nov 18 '22 21:11

Robert Rohm