0

JavaFX 的默认选择模型ListView似乎会向Change对象中的侦听器报告错误的值,如果选择模式为SelectionMode.MULTIPLE. 我的解释Change不正确吗?

我有一个包含“Julia”、“Ian”、“Sue”等项目的列表视图。下面的代码生成以下输出:

点击第一项“Julia”(“Julia”被选中,输出正确):

from 0, to 1, removed 0, added 1
was added
removed:
added:
    Julia
selected:
    Julia

Shift + ArrowDown(选择“Julia”和“Ian”):

from 0, to 1, removed 0, added 1    <-- should be from 1, to 2
was added
removed:
added:
    Julia    <-- should be Ian
selected:
    Julia
    Ian

Shift + ArrowDown(选择“Julia”、“Ian”和“Sue”):

from 0, to 1, removed 0, added 1    <-- should be from 2, to 3
was added
removed:
added:
    Julia    <-- should be Sue
selected:
    Julia
    Ian
    Sue

对这种行为有什么解释吗?

public void start(Stage stage) {
    ObservableList<String> items = FXCollections.observableArrayList(
            "Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise");
    ListView<String> listView = new ListView<>(items);

    MultipleSelectionModel<String> selectionModel = listView.getSelectionModel();
    selectionModel.setSelectionMode(SelectionMode.MULTIPLE);
    ObservableList<String> selectedItems = selectionModel.getSelectedItems();

    selectedItems.addListener((ListChangeListener.Change<? extends String> c) -> {
        while (c.next()) {
            System.out.printf("from %d, to %d, removed %d, added %d\n", 
                    c.getFrom(), c.getTo(), c.getRemovedSize(), c.getAddedSize());
            if (c.wasAdded()) System.out.println("was added");
            if (c.wasRemoved()) System.out.println("was removed");
            if (c.wasPermutated()) System.out.println("was permutated");
            if (c.wasUpdated()) System.out.println("was updated");
            if (c.wasReplaced()) System.out.println("was replaced");
            System.out.println("removed:");
            for (String e : c.getRemoved()) {
                System.out.println("    " + e);
            }
            System.out.println("added:");
            for (String e : c.getAddedSubList()) {
                System.out.println("    " + e);
            }
            System.out.println("selected:");
            for (String e : selectedItems) {
                System.out.println("    " + e);
            }
            System.out.println();
        }
    });

    Scene scene = new Scene(listView, 300, 300);
    stage.setTitle("List View Sample");
    stage.setScene(scene);
    stage.show();
}
4

1 回答 1

1

这看起来像一个错误,可能在ListView. 您应该在https://javafx-jira.kenai.com提交错误报告

注意

ObservableList<String> names = FXCollections.observableArrayList();
names.addListener((ListChangeListener.Change<? extends String> c) -> {
        while (c.next()) {
            System.out.printf("from %d, to %d, removed %d, added %d\n", 
                    c.getFrom(), c.getTo(), c.getRemovedSize(), c.getAddedSize());
            if (c.wasAdded()) System.out.println("was added");
            if (c.wasRemoved()) System.out.println("was removed");
            if (c.wasPermutated()) System.out.println("was permutated");
            if (c.wasUpdated()) System.out.println("was updated");
            if (c.wasReplaced()) System.out.println("was replaced");
            System.out.println("removed:");
            for (String e : c.getRemoved()) {
                System.out.println("    " + e);
            }
            System.out.println("added:");
            for (String e : c.getAddedSubList()) {
                System.out.println("    " + e);
            }
            System.out.println("selected:");
            for (String e : names) {
                System.out.println("    " + e);
            }
            System.out.println();
        }
    });
names.add("Julia");
names.add("Ian");
names.add("Sue");

行为如你所料。

于 2014-05-02T22:59:16.573 回答