【问题标题】:Binding the sorting behavior of TableColumns between two TableViews in JavaFX在 JavaFX 中的两个 TableView 之间绑定 TableColumns 的排序行为
【发布时间】:2018-08-12 00:45:50
【问题描述】:

如果有人问过这个问题,但我的搜索没有成功,我深表歉意。

给定两个在 JavaFX 中列数和类型相同的表,例如:

// First Table
private TableView table1 = new TableView();

TableColumn col11 = new TableColumn("col1");
TableColumn col12 = new TableColumn("col2");

table1.getColumns().addAll(col11, col12);


// Second Table
private TableView table2 = new TableView();

TableColumn col21 = new TableColumn("col1");
TableColumn col22 = new TableColumn("col2");

table2.getColumns().addAll(col21, col22);

[... logic adding and showing tables here ...]

如何绑定col11col21 以及col21col22 之间的排序属性,这样,当我单击table1 中的列标题时(使排序箭头出现),table2 将根据该列的条件进行排序。

【问题讨论】:

    标签: java sorting javafx tableview tablecolumn


    【解决方案1】:

    为此,您需要绑定表格列的sortType 属性,并确保表格的sortOrder 包含相应的列:

    /**
      * holder for boolean variable to be passed as reference
      */
    private static class BoolContainer {
        boolean value;
    }
    
    public static <T> void createSortBinding(TableView<T> table1, TableView<T> table2) {
        final int count = table1.getColumns().size();
        if (table2.getColumns().size() != count) {
            throw new IllegalArgumentException();
        }
    
        // bind sort types
        for (int i = 0; i < count; i++) {
            table1.getColumns().get(i).sortTypeProperty().bindBidirectional(
                    table2.getColumns().get(i).sortTypeProperty());
        }
    
        BoolContainer container = new BoolContainer();
        bindList(table1, table2, container);
        bindList(table2, table1, container);
    }
    
    /**
      * helper for one direction of the binding of the source orders
      */
    private static <T> void bindList(TableView<T> table1,
            TableView<T> table2,
            BoolContainer modifyingHolder) {
    
        ObservableList<TableColumn<T, ?>> sourceColumns = table1.getColumns();
        ObservableList<TableColumn<T, ?>> sourceSortOrder = table1.getSortOrder();
        ObservableList<TableColumn<T, ?>> targetColumns = table2.getColumns();
        ObservableList<TableColumn<T, ?>> targetSortOrder = table2.getSortOrder();
    
        // create map to make sort order independent form column order
        Map<TableColumn<T, ?>, TableColumn<T, ?>> map = new HashMap<>();
        for (int i = 0, size = sourceColumns.size(); i < size; i++) {
            map.put(sourceColumns.get(i), targetColumns.get(i));
        }
    
        sourceSortOrder.addListener((Observable o) -> {
            if (!modifyingHolder.value) {
                modifyingHolder.value = true;
    
                // use map to get corresponding sort order of other table
                final int size = sourceSortOrder.size();
                TableColumn<T, ?>[] sortOrder = new TableColumn[size];
    
                for (int i = 0; i< size; i++) {
                    sortOrder[i] = map.get(sourceSortOrder.get(i));
                }
                targetSortOrder.setAll(sortOrder);
    
                modifyingHolder.value = false;
            }
        });
    }
    

    用法:

    createSortBinding(table1, table2);
    

    【讨论】:

    • 完美无瑕!感谢您快速准确的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多