JavaFX Tableview按自定义规则排序,然后按列选择排序

时间:2018-05-01 00:08:08

标签: java sorting javafx

我正在尝试对包含3列的JavaFX TableView进行排序,其中一列是日期,一列是名称(字符串),最后一列是标记列(枚举)。我想要做的就是使它无论表当前正在哪个列上排序,行首先按标签排序(如果它有某个标签,则在行上方排序)没有那个标签的那个。)

因此,当按名称升序搜索时,表格的顺序如下:

  • 'George'[tag]
  • 'ZZ'[tag]
  • 'Apple'[no-tag]
  • '你好'[no-tag]

我查看了列比较器,但是我似乎只能为该列类型指定,即我希望能够指定名称 - 列比较器来接受整行的类对象,并且名称 - 列数据类型(字符串),因此我可以访问该类实例中的标记 - 但是在线查看后似乎不可能。

即使选择了标签列来命令desc(因此它仍然放置带有标记的行),是否也可以保留此规则。如果不是,我可以禁用标记列的排序

提前感谢任何能指出正确方向的人

2 个答案:

答案 0 :(得分:2)

正如我在评论中提到的,您不能只创建一个绑定,该绑定将TableView的比较器包装并提供给SortedList。另外SortedList是final,因此无法扩展它。另外,使用自定义TransformationList也不起作用,因为TableView具有一些以if (itemsList instanceof SortedList)开头的硬编码内容。 这就是我最终的目的(为了清楚起见,使用java7格式,而不是labdas):

    filteredList = new FilteredList<>(yourOriginalList);
    table.sortPolicyProperty().set(new Callback<TableView<YourObject>, Boolean>() {
        @Override
        public Boolean call(TableView<YourObject> param) {
            final Comparator<YourObject> tableComparator = transactionTable.getComparator();
            // if the column is set to unsorted, tableComparator can be null
            Comparator<YourObject> comparator = tableComparator == null ? null : new Comparator<YourObject>() {
                @Override
                public int compare(YourObject o1, YourObject o2) {
                    // first sort by your tag
                    final int tagCompare = o1.getTag().compareTo(o2.getTag());
                    if (tagCompare == 0) {
                        // secondly sort by the comparator that was set for the table
                        return tableComparator.compare(o1, o2);
                    }
                    return tagCompare;
                }
            };
            table.setItems(filteredList.sorted(comparator));
            return true;
        }
    });

    // you may also need to call
    table.setItems(filteredList.sorted(transactionTable.getComparator()));

答案 1 :(得分:1)

我同意@Miklos解决方案。以下是您的要求(基于@Miklos解决方案)的完整工作示例,该要求首先对“标记”列进行排序,然后对所选列进行排序。 [这不是一个完整的解决方案,但这应该为您提供一些指导,使其完全按照您的需要工作]

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static javafx.scene.control.TableColumn.SortType.ASCENDING;

public class CustomComparatorTableColumn extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        ObservableList<Device> devices = FXCollections.observableArrayList();
        devices.add(new Device(1, "Apple", "Zebra", TagType.TAG));
        devices.add(new Device(2, "BlackBerry", "Parrot", TagType.TAG));
        devices.add(new Device(3, "Amazon", "Yak", TagType.NO_TAG));
        devices.add(new Device(4, "Oppo", "Penguin", TagType.NO_TAG));

        TableView<Device> tableView = new TableView<>();
        TableColumn<Device, String> nameCol = new TableColumn<>("Name");
        nameCol.setCellValueFactory(param -> param.getValue().nameProperty());

        TableColumn<Device, String> displayCol = new TableColumn<>("Display");
        displayCol.setCellValueFactory(param -> param.getValue().displayProperty());

        TableColumn<Device, TagType> tagCol = new TableColumn<>("Tag");
        tagCol.setCellValueFactory(param -> param.getValue().tagTypeProperty());

        tableView.getColumns().addAll(nameCol, displayCol, tagCol);
        tableView.setItems(devices);

        tableView.setSortPolicy(tv -> {
            final ObservableList<Device> itemsList = tableView.getItems();
            if (itemsList == null || itemsList.isEmpty()) {
                return true;
            }
            final List<TableColumn<Device, ?>> sortOrder = new ArrayList<>(tableView.getSortOrder());
            if (!sortOrder.isEmpty()) {
                // If there is no Tag column in the sort order, always adding as the first sort to consider.
                if (!sortOrder.stream().anyMatch(tc -> tc.getText().equals("Tag"))) {
                    sortOrder.add(0, tagCol);
                }
                FXCollections.sort(itemsList, new TableColumnComparator<>(sortOrder));
            }
            return true;
        });

        Scene sc = new Scene(tableView);
        primaryStage.setScene(sc);
        primaryStage.show();

    }

    class TableColumnComparator<S> implements Comparator<S> {
        private final List<TableColumn<S, ?>> allColumns;

        public TableColumnComparator(final List<TableColumn<S, ?>> allColumns) {
            this.allColumns = allColumns;
        }

        @Override
        public int compare(final S o1, final S o2) {
            for (final TableColumn<S, ?> tc : allColumns) {
                if (!isSortable(tc)) {
                    continue;
                }
                final Object value1 = tc.getCellData(o1);
                final Object value2 = tc.getCellData(o2);

                @SuppressWarnings("unchecked") final Comparator<Object> c = (Comparator<Object>) tc.getComparator();
                final int result = ASCENDING.equals(tc.getSortType()) ? c.compare(value1, value2)
                        : c.compare(value2, value1);

                if (result != 0) {
                    return result;
                }
            }
            return 0;
        }

        private boolean isSortable(final TableColumn<S, ?> tc) {
            return tc.getSortType() != null && tc.isSortable();
        }
    }

    class Device {
        IntegerProperty id = new SimpleIntegerProperty();
        StringProperty name = new SimpleStringProperty();
        StringProperty display = new SimpleStringProperty();
        ObjectProperty<TagType> tagType = new SimpleObjectProperty<>();

        public Device(int id, String n, String d, TagType tag) {
            this.id.set(id);
            name.set(n);
            tagType.set(tag);
            display.set(d);
        }

        public String getName() {
            return name.get();
        }

        public StringProperty nameProperty() {
            return name;
        }

        public void setName(String name) {
            this.name.set(name);
        }

        public ObjectProperty<TagType> tagTypeProperty() {
            return tagType;
        }

        public StringProperty displayProperty() {
            return display;
        }

    }

    enum TagType {
        TAG, NO_TAG;
    }
}