How to get current cell value from cell factory?
Asked Answered
J

1

6

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?

Jewelfish answered 22/8, 2015 at 10:0 Comment(1)
What is CellShape? Is it a TableCell subclass?Titulary
M
6

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 ...

        // ...
      }
    };
  }
});
Monegasque answered 22/8, 2015 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.