我的 cellFactory 有问题。 我希望如果将日期设置为 FX TableView 内的对象,则 updateItem 方法检查它是否是有效日期。如果不是,单元格应为红色。
_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("");
}
}
}
};
});
着色有效,但 LocalDate 从未设置为单元格。据我了解,这应该发生在 super() 调用中。 为此列实现了 CellValueFactory:
_availableCol.setCellValueFactory(new PropertyValueFactory<>("Available"));
知道我做错了什么吗?
最佳答案
您需要设置节点的文本。当您检测到它为空时,您会清除它,但当它不为空时,您不会设置它。如果单元格为空,您还会忘记清除样式。
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);
}
关于JavaFX setCellFactory 未设置我的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50031138/