英文:
JavaFX Tableview sort java.sql.Date column with null at bottom on ascending order
问题
// 有一列包含java.sql.Date,我想按升序将空值放在底部:
[![enter image description here][1]][1]
现在,它们最终出现在顶部。
followupCol.setCellFactory((TableColumn<DisabilityCase, Date> p) -> {
TableCell<DisabilityCase, Date> cell = new TableCell<DisabilityCase, Date>() {
@Override
protected void updateItem(Date item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
if (!LocalDate.now().isBefore(item.toLocalDate())) {
setTextFill(Color.RED);
} else {
setTextFill(Color.BLACK);
}
setText(new SimpleDateFormat(DATE_FORMAT).format(item));
}
}
};
return cell;
});
<details>
<summary>英文:</summary>
I have a column that contains java.sql.Date and I want it to put nulls at the bottom in ascending order:
[![enter image description here][1]][1]
Right now, they end up on top.
followupCol.setCellFactory((TableColumn<DisabilityCase, Date> p) ->
{
TableCell<DisabilityCase, Date> cell = new TableCell<DisabilityCase, Date>()
{
@Override
protected void updateItem(Date item, boolean empty)
{
super.updateItem(item, empty);
if (item == null || empty)
{
setText(null);
} else
{
if (!LocalDate.now().isBefore(item.toLocalDate()))
{
setTextFill(Color.RED);
} else
{
setTextFill(Color.BLACK);
}
setText(new SimpleDateFormat(DATE_FORMAT).format(item));
}
}
};
return cell;
});
[1]: https://i.stack.imgur.com/aojM7.png
</details>
# 答案1
**得分**: 7
因为 `java.sql.Date` 已经实现了 `Comparable` 接口,所以你可以使用
```java
followupCol.setComparator(Comparator.nullsLast(Comparator.naturalOrder()));
英文:
Since java.sql.Date
is already Comparable
, you can use
followupCol.setComparator(Comparator.nullsLast(Comparator.naturalOrder()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论