Reputation: 13
I have TableView<TypeReport> tv
and column
tc_name.setCellValueFactory(new PropertyValueFactory<>("name"));
I want rows to be bold if date in cell of tc_name
does not starts with " "
.
I use:
tv.setRowFactory(new Callback<TableView<TypeReport>, TableRow<TypeReport>>() {
@Override
public TableRow<TypeReport> call(TableView<TypeReport> param) {
return new TableRow<TypeReport>() {
@Override
protected void updateItem(TypeReport item, boolean empty) {
if (item==null) {
setStyle("");
} else {
String res=item.getName();
if (res.startsWith(" ")){}
else
setStyle("-fx-font-weight: bold");
}
}
};
}
});
When I open app, required rows are bold, but when I scroll, some differ rows are bold.
Upvotes: 1
Views: 2918
Reputation: 209418
You need to remove the style for rows whose item's name starts with " "
. (Note you also need to call super.updateItem(...)
):
@Override
protected void updateItem(TypeReport item, boolean empty) {
super.updateItem(item, empty);
if (item==null) {
setStyle("");
} else {
String res=item.getName();
if (res.startsWith(" ")) {
setStyle("");
} else
setStyle("-fx-font-weight: bold");
}
}
or, equivalently (but with less code):
@Override
protected void updateItem(TypeReport item, boolean empty) {
super.updateItem(item, empty);
if (item==null || item.getName().startsWith(" ")) {
setStyle("");
} else {
setStyle("-fx-font-weight: bold");
}
}
Upvotes: 3