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();
}