JavaFX Tableview sort java.sql.Date column with null at bottom on ascending order

Viewed 102

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

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 Answers

Since java.sql.Date is already Comparable, you can use

followupCol.setComparator(Comparator.nullsLast(Comparator.naturalOrder()));
Related