【发布时间】:2018-02-12 18:34:22
【问题描述】:
我有一个视图,在那个视图中有几个 tableViews,但假设我有两个表。当我重新排序第一个表中的列时(通过使用拖放),我想在第二个表中做同样的事情。我在尝试某种方式,但我得到了
IllegalStateException: Duplicate TreeTableColumns detected in TreeTableView columns list with titles ''
这是我试过的:
private void handleDragAndDrop() {
firstTable.getColumns().addListener((ListChangeListener<? super TableColumn<?, ?>>) c -> {
while (c.next()) {
List<String> addedIDs = c.getAddedSubList()
.stream()
.map(TableColumn::getId)
.collect(Collectors.toList());
Map<String, String> changedIds = getChangedIds(addedIDs);
changedIds.entrySet()
.stream()
.reduce((firstEntry, lastEntry) -> lastEntry)
.ifPresent(entry-> reorderTheSecondTable(secondTable.getColumns(),entry.getValue(),
entry.getKey()));
}
});
}
private Map<String, String> getChangedIds(List<String> iDs) {
return new LinkedHashMap<>(iDs.stream()
.skip(1) // I don't need the first column, that will be never reordered.
.filter(id -> !id.equals(String.valueOf(iDs.indexOf(id)))) // I need only the changed id-index pairs.
.collect(Collectors.toMap(i -> String.valueOf(iDs.indexOf(i)), Function.identity())));
}
private void reorderTheSecondTable(ObservableList<? extends TableColumnBase<?, ?>> list, String start, String end) {
Collections.rotate(list.subList(Integer.valueOf(start), Integer.valueOf(end) + 1), 1);
}
列的 id 从 1 到 table.getColumns().size()-1(0 是不可变列)
异常在Collections.rotate(...行抛出
我不知道这是否是正确的做法,但我没有找到更好的解决方案,甚至这也不是解决方案,因为它不起作用。
【问题讨论】:
-
两个 TableView 是否映射同一个对象?
-
没有第一个表有
TableView<FirstTablesData> firstTable,第二个表是TreeTableView<SecondTableData> secondTable -
两个 TableView 中的列数是否相同?
-
是的,列的数量始终相同,或者更准确地说,每个表的列数都相同。
标签: java javafx javafx-8 bind tablecolumn