I recently researched this topic a bit. With the following code, you can change the color of the TableView row based on the column value (I will try to explain this as best as possible).
The first thing we need to do is define the TableView and the columns of this TableView:
private TableView<Person> personTable;
private TableColumn<Person, String> nameColumn;
private TableColumn<Person, String> lastNameColumn;
The next step is to determine the Cell Factory of one of the columns:
nameColumn.setCellFactory(column -> {
return new TableCell<Person, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setStyle("");
} else {
setText(item);
Person auxPerson = getTableView().getItems().get(getIndex());
if (auxPerson.getName().equals("Edgard")) {
setTextFill(Color.RED);
setStyle("-fx-background-color: yellow");
} else {
if(getTableView().getSelectionModel().getSelectedItems().contains(auxPerson))
setTextFill(Color.WHITE);
else
setTextFill(Color.BLACK);
}
}
}
};
});
: updateItem(), , .
( String), . (, ), . , , , .
, "Row Factory" "Cell Factory", :
personTable.setRowFactory(row -> new TableRow<Person>(){
@Override
public void updateItem(Person item, boolean empty){
super.updateItem(item, empty);
if (item == null || empty) {
setStyle("");
} else {
if (item.getName().equals("Edgar")) {
for(int i=0; i<getChildren().size();i++){
((Labeled) getChildren().get(i)).setTextFill(Color.RED);
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: yellow");
}
} else {
if(getTableView().getSelectionModel().getSelectedItems().contains(item)){
for(int i=0; i<getChildren().size();i++){
((Labeled) getChildren().get(i)).setTextFill(Color.WHITE);;
}
}
else{
for(int i=0; i<getChildren().size();i++){
((Labeled) getChildren().get(i)).setTextFill(Color.BLACK);;
}
}
}
}
}
});
. getTableRow() Factory, .
1. , . , , .
2. CSS, - :
.table-cell {
-fx-text-fill: Black;
}
, , Java .