JavaFX TableView按升序排序java.sql.Date列,并将空值放在底部。

huangapple go评论62阅读模式
英文:

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&lt;DisabilityCase, Date&gt; p) -&gt;
            {
                TableCell&lt;DisabilityCase, Date&gt; cell = new TableCell&lt;DisabilityCase, Date&gt;()
                {
                    @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()));

huangapple
  • 本文由 发表于 2020年5月30日 03:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/62093018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定