【问题标题】:JavaFX TableView Ignore empty cells when sortingJavaFX TableView 排序时忽略空单元格
【发布时间】:2018-11-14 03:17:14
【问题描述】:

当有空单元格时,我在对列进行排序时遇到问题。

我为我的codeMed 列创建了一个新的Comparator:

codeMed.setComparator(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {

            if (o1 == null)return -1;
            if (o2 == null) return -1;
            return o1 < o2 ? -1 : o1 == o2 ? 0 :1;
        }
    });

起初,它似乎工作正常:

但如果我决定对不同的列进行排序,然后对 codeMed 列进行排序,就会发生这种情况:

我想错误出在比较器中,但我不知道是什么问题。

编辑:我希望空值始终位于列的底部。我试过这样的事情:

if (codeMed.getSortType() == TableColumn.SortType.DESCENDING) {
    return (o1 != null ? o1 : Integer.MAX_VALUE) - (o2 != null ? o2 : Integer.MAX_VALUE);
} else if (codeMed.getSortType()==TableColumn.SortType.ASCENDING){
    return (o1 != null ? o1 : Integer.MIN_VALUE) - (o2 != null ? o2 : Integer.MIN_VALUE);
}
return 0;

但它不起作用:/(可能是因为 Slaw 建议的问题)

我的解决方案:

非常感谢 Jai,我调整您的代码只是因为我想将它用于 2 个不同的列:

Comparator<Integer> integerComparator = new Comparator<>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            final boolean isDesc = tabInfosPatient.getSortOrder().get(0).getSortType() == TableColumn.SortType.DESCENDING;
            if (o1 == null && o2 == null) return 0;
            else if (o1 == null) return isDesc ? -1 : 1;
            else if (o2 == null) return isDesc ? 1 : -1;
            else return Integer.compare(o1, o2);
        }
    };

【问题讨论】:

    标签: java javafx tableview


    【解决方案1】:

    我很惊讶它甚至可以工作一次。这是我认为会很好用的方法:

    codeMed.setComparator(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return (o1 != null ? o1 : Integer.MAX_VALUE) - (o2 != null ? o2 : Integer.MAX_VALUE);
            }
        });
    

    这将假定任何null 值都被视为代表最大可能的int 值。这将迫使它移动到升序列中的列表底部,或者如果它位于降序列中,则移动到顶部。如果需要相反,请将Integer.MAX_VALUE 切换为Integer.MIN_VALUE。

    你所做的没有用,因为你违反了这个:

    实现者必须确保 sgn(compare(x, y)) == -sgn(compare(y, x)) 对于所有 x 和 y。 (这意味着 compare(x, y) 必须抛出一个 异常当且仅当 compare(y, x) 抛出异常。)

    当o1 是5 并且o2 是null 时,就是这种违规的一个例子。在这种情况下,compare(o1, o2) 返回 -1,compare(o2, o1) 也返回 -1。其中一个应该返回正值,而另一个负值,或者两者都必须返回0。

    更新

    这就是你需要的。

    public class Model {
        private final ObjectProperty<Integer> v = new SimpleObjectProperty<>();
    
        public Model(Integer v) {
            this.v.setValue(v);
        }
    
        public final ObjectProperty<Integer> vProperty() {
            return this.v;
        }
    
        public final Integer getV() {
            return this.vProperty().get();
        }
    
        public final void setV(final Integer v) {
            this.vProperty().set(v);
        }
    }
    
    ObservableList<Model> list = FXCollections.observableArrayList();
    list.addAll(new Model(20), new Model(-30), new Model(null), new Model(10));
    
    TableView<Model> tv = new TableView<>();
    TableColumn<Model, Integer> tc = new TableColumn<>();
    tc.setCellValueFactory(new PropertyValueFactory<>("v"));
    tv.getColumns().add(tc);
    tv.setItems(list);
    
    Comparator<Integer> ascComparator = (o1, o2) ->
                                    (o1 != null ? o1 : Integer.MAX_VALUE) - 
                                    (o2 != null ? o2 : Integer.MAX_VALUE);
    
    Comparator<Integer> descComparator = (o1, o2) ->
                                    (o1 != null ? o1 : Integer.MIN_VALUE) - 
                                    (o2 != null ? o2 : Integer.MIN_VALUE);
    
    @SuppressWarnings("unchecked")
    Comparator<Integer> defaultComparator = TableColumn.DEFAULT_COMPARATOR;
    
    tc.comparatorProperty().bind(
            Bindings.when(tc.sortTypeProperty().isEqualTo(SortType.ASCENDING))
                    .then(ascComparator)
                    .otherwise(
                            Bindings.when(tc.sortTypeProperty().isEqualTo(SortType.DESCENDING))
                                    .then(descComparator)
                                    .otherwise(defaultComparator)));
    

    另外,我想指出,虽然使用 Integer.MIN_VALUE 和 Integer.MAX_VALUE 应该在大多数情况下工作,但整数下溢和溢出问题的风险要高得多,我不确定这是否是使用比较器和比较器的问题。

    如果你想更安全,那就做一堆if-else:

    Comparator<Integer> ascComparator = (o1, o2) -> {
        if (o1 == null && o2 == null) return 0;
        else if (o1 == null && o2 != null) return -1;
        else if (o1 != null && o2 == null) return 1;
        else return Integer.compare(o1, o2);
    };
    

    再次更新

    看了你的尝试后,我意识到这是可行的:

    Comparator<Integer> comparator = (o1, o2) -> {
        final boolean isDesc = tc.getSortType() == SortType.DESCENDING;
        if (o1 == null && o2 == null) return 0;
        else if (o1 == null && o2 != null) return isDesc ? -1 : 1;
        else if (o1 != null && o2 == null) return isDesc ? 1 : -1;
        else return Integer.compare(o1, o2);
    };
    

    【讨论】:

    • 感谢您的回复。有没有办法让空值始终位于底部?
    • 我不确定返回值的大小是否重要,只有当它是负数、零或正数时。 @Couldosh 返回结果表明空值大于非空值,并且在排序时空值将移到底部。
    • 我编辑了我的问题,试图在底部始终有 te 空值
    • @Couldosh 更新了答案。
    猜你喜欢
    • 2015-08-02
    • 2014-11-14
    • 1970-01-01
    • 2011-10-18
    • 2012-08-30
    • 1970-01-01
    • 2013-05-10
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多