Reputation: 116
I have got a problem with my cellFactory. I want that if a Date is set to my object inside a FX TableView, that the updateItem method checks if it is a valid date. If not the cell should be colored red.
_availableCol.setCellFactory(column -> {
return new TableCell<SimpleReservationUnit, LocalDate>() {
@Override
protected void updateItem(LocalDate date, boolean empty) {
super.updateItem(date, empty);
if (empty || date == null) {
setText(null);
}else {
if (date.compareTo(_newDeparture.getValue()) < 0) {
setStyle("-fx-background-color: red");
}else{
setStyle("");
}
}
}
};
});
The coloring works, but the LocalDate is never set to the cell. As far as I understand this should happen in the super() call. A CellValueFactory is implemeted for this column:
_availableCol.setCellValueFactory(new PropertyValueFactory<>("Available"));
Any ideas what i am doing wrong?
Upvotes: 0
Views: 997
Reputation: 3563
You need to set the text of the node. You do clear it when you detect it is empty, but you don't set it when it is not. You also forget to clear the style if the cell is empty.
if (empty || date == null) {
setText(null);
setStyle(""); // can this be null?
} else {
LocalDate departureDate = _newDeparture.getValue();
String text = departureDate.toString(); // or format it for user locale
setText(text);
String style = date.isBefore(departureDate) ? "-fx-background-color: red" : "";
setStyle(style);
}
Upvotes: 1