See Programmatically change the TableView row appearance for a fuller discussion of this topic.
Since cells are reused, you need to make sure you only add the style class once to each cell. (In your current code, if the item changes from one for which your condition is true to another for which your condition is true, the list of style classes will contain a duplicate "priorityLow" entry. Eventually, you could potentially run out of memory by replicating this enough.)
Additionally, you need to remove any occurrences of "priorityLow" if your condition is false. (In your current code, if the item changes from one for which your condition is true to one for which your condition is false, the "priorityLow" class will remain incorrectly attached.)
Something like this:
personTable.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
@Override
public TableRow<Person> call(TableView<Person> personTableView) {
return new TableRow<Person>() {
@Override
protected void updateItem(Person person, boolean b) {
super.updateItem(person, b);
if(.../*my requirements*/) {
ObservableList<String> styles = getStyleClass();
if (! styles.contains("priorityLow")) {
getStyleClass().add("priorityLow");
}
} else {
// Remove all occurrences of "priorityLow":
styles.removeAll(Collections.singleton("priorityLow"));
}
}
};
}
});
If you are using JavaFX 8, a solution using PseudoClasses will be cleaner and more efficient:
Update PseudoClass version (Java 8 only):
final PseudoClass lowPriorityPseudoClass = PseudoClass.getPseudoClass("priority-low");
personTable.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
@Override
public TableRow<Person> call(TableView<Person> personTableView) {
return new TableRow<Person>() {
@Override
protected void updateItem(Person person, boolean b) {
super.updateItem(person, b);
boolean lowPriority = /* my requirements */ ;
pseudoClassStateChanged(lowPriorityPseudoClass, lowPriority);
}
};
}
});
and the css will look like:
.table-row-cell:priority-low {
/* styles specific for low priority row */
}