0

基本值列表由(变化的)谓词过滤。被FilteredList映射到TreeItems,然后这个结果列表被用作根TreeItems 的孩子。

当在 上进行选择时TreeTableView,然后谓词更改,访问所选项目会导致NullPointerException.

在我看来,更改中包含的项目是null. 这个粗略的概念是否存在设计缺陷?

这不会发生在类TreeViewListView.

我尝试使用https://github.com/TomasMikula/EasyBind为映射生成 MCVE:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.fxmisc.easybind.EasyBind;

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Spinner;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    // fields protect bound lists from GC
    private ObservableList<DataItem> itemizedDataPool;
    private FilteredList<Data> filteredDataPool;
    private ObservableList<Data> selectedData;

    static class Data {
        final int value;

        public Data(int value) {
            this.value = value;
        }
    }

    static class DataItem extends TreeItem<Data> {
        final Data data;

        public DataItem(Data data) {
            this.data = data;
        }
    }

    @Override
    public void start(Stage primaryStage) throws IOException {

        List<Data> dataPool = new ArrayList<Data>();
        for (int i = 1; i < 20; i++) {
            dataPool.add(new Data(i));
        }

        filteredDataPool = new FilteredList<>(FXCollections.observableArrayList(dataPool));

        TreeTableView<Data> listView = createTreeTableView();
        Spinner<?> lowerBoundSelector = createLowerBoundFilter();
        Label sumLabel = createSummarizingLabel(listView.getSelectionModel().getSelectedItems());

        Parent root = new VBox(listView, lowerBoundSelector, sumLabel);

        Scene scene = new Scene(root, 768, 480);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TreeTableView<Data> createTreeTableView() {
        itemizedDataPool = EasyBind.map(filteredDataPool, DataItem::new);
        TreeItem<Data> itemRoot = new TreeItem<>();
        Bindings.bindContent(itemRoot.getChildren(), itemizedDataPool);

        TreeTableView<Data> listView = new TreeTableView<>(itemRoot);
        listView.setShowRoot(false);
        itemRoot.setExpanded(true);
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        listView.getColumns().add(new TreeTableColumn<>("Data"));
        return listView;
    }

    private Label createSummarizingLabel(ObservableList<TreeItem<Data>> selectedItems) {
        Label sumLabel = new Label();
        selectedData = EasyBind.map(selectedItems, (TreeItem<Data> t) -> ((DataItem) t).data);
        selectedData.addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                int sum = 0;
                for (Data d : selectedData) {
                    sum += d.value;
                }
                sumLabel.setText("Sum: " + sum);
            }
        });
        return sumLabel;
    }

    private Spinner<Integer> createLowerBoundFilter() {
        Spinner<Integer> lowerBoundSelector = new Spinner<>(0, 20, 0, 1);
        lowerBoundSelector.valueProperty().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                filteredDataPool.setPredicate(t -> t.value > lowerBoundSelector.getValue());
            }
        });
        return lowerBoundSelector;
    }

    public static void main(String[] args) {
        launch(args);
    }

}
4

1 回答 1

0

问题

TreeTableViewuses TreeTableViewArrayListSelectionModel, which extends MultipleSelectionModelBase, which uses ReadOnlyUnbackedObservableList, which uses (and contains) SelectionListIterator, which has a broken implementation for its method nextIndex

感谢fabian指出这一点。他还提交了一份错误报告 ( http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8145887 )。

解决方法

在两者之间使用缓冲区可以为上述问题提供有效的解决方法。我尝试了几种方法。setAll在选择失效和Bindings.bindContent 不工作。在这两种情况下,我都收到null了列表中的值。直接的“解决方案”是简单地将nulls 过滤掉。这导致下面的代码效率低下但显然有效。

// [...]
TreeTableView<Data> listView = createTreeTableView();
selectionBuffer = FXCollections.observableArrayList();
listView.getSelectionModel().getSelectedItems().addListener(new InvalidationListener() {
    @Override
    public void invalidated(Observable observable) {
        selectionBuffer.clear();
        for (TreeItem<Data> t : listView.getSelectionModel().getSelectedItems()) {
            if (t != null) {
                selectionBuffer.add(t);
            }
        }
    }
});
// [...]

使用selectionBuffer代替listView.getSelectionModel().getSelectedItems()现在应该可以弥补nextIndex.

于 2016-03-04T11:38:55.290 回答