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
?
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 ...
// ...
}
};
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With