问题
I updated my code to show how the whole class look like Still I got some errors can you validate what I need to improve to have it working
Mainly the problem is with fillTableView() method
private TableCell fillTableView() {
clientColumn.setCellFactory(column -> {
return new TableCell<CarFx, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow<CarFx> currentRow = getTableRow();
if (!isEmpty()) {
if (item.equals("EMPTY EMPTY"))
currentRow.setStyle("-fx-background-color:green");
else
currentRow.setStyle("-fx-background-color:blue");
}
}
};
});
}
回答1:
You need to update the items of Table View by overriding updateItem()
method.
The code is :
yourColumnBased.setCellFactory(column -> {
return new TableCell<CarFx, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow<CarFx> currentRow = getTableRow();
if (!isEmpty()) {
if(item.equals("assigned"))
currentRow.setStyle("-fx-background-color:blue");
else
currentRow.setStyle("-fx-background-color:green");
}
}
};
});
来源:https://stackoverflow.com/questions/53251956/java-fx-filling-table-rows-in-different-colours