JavaFX ForEach循环变量保存ObservableList之外的值
我有一个ObservableList,一个保存它的ListView和一个将所选项目更新为标签的事件JavaFX ForEach循环变量保存ObservableList之外的值,java,javafx,Java,Javafx,我有一个ObservableList,一个保存它的ListView和一个将所选项目更新为标签的事件 ObservableList<String> items = FXCollections.observableArrayList( "China", "Japan", "Korea", "India"); ListView<String> lvCountries = new ListView<>(); lvCountries.setIte
ObservableList<String> items = FXCollections.observableArrayList(
"China", "Japan", "Korea", "India");
ListView<String> lvCountries = new ListView<>();
lvCountries.setItems(items);
lvCountries.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lvCountries.getSelectionModel().selectedIndexProperty().addListener(observable -> {
ObservableList<Integer> indices = lvCountries.getSelectionModel().getSelectedIndices();
if (indices.size() != 0) {
StringBuilder stringBuilder = new StringBuilder("Selected items are");
indices.forEach(index -> stringBuilder.append(" " + items.get(index)));
lblSelected.setText(stringBuilder.toString());
}
});
observeList items=FXCollections.observearraylist(
“中国”、“日本”、“韩国”、“印度”);
ListView lvCountries=新建ListView();
lvCountries.setItems(items);
lvCountries.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lvCountries.getSelectionModel().selectedIndexProperty().addListener(可观察->{
ObservableList Index=lvCountries.getSelectionModel().GetSelectedDices();
如果(index.size()!=0){
StringBuilder StringBuilder=新建StringBuilder(“所选项目为”);
index.forEach(index->stringBuilder.append(“+items.get(index));
lblSelected.setText(stringBuilder.toString());
}
});
一切看起来都很好,但当我高速随机单击时,有时会抛出ArrayIndexOutOfBoundsException,因为索引的值为-1。同样,只有在ListView的多种模式下,在项目之间随机快速单击时,才会发生这种情况
我不确定这是否与ObservableList有关,也不确定foreach循环如何工作或索引。该程序没有崩溃或其他任何问题,但我想了解为什么会发生这种情况以及如何发生这种情况以及解决方案
我使用的是Java8更新181(Build1.8.0_181-b13)、IntellijIdeaCE、MacOS10.14
提前谢谢,抱歉英语不好 在SelectedDices的内容上有一些bug,有些已经修复,有些还没有。请提供一个演示问题并添加您的fx版本的示例,这样我们就可以确定问题的位置(您的代码或核心fx,两者都有可能:)@Matt正如我所说,只有在高速随机单击时才会发生这种情况。哦,我忘了提到它是在选择题模式下吗?将立即更新。我已经能够复制该问题。当您按住shift键并选择多个图元,然后选择一个图元时,会发生这种情况。在这种情况下,索引列表包含-1。出于某种奇怪的原因(可能是由于kleopatra指出的一个bug),如果您遍历索引列表两次,第二次它就会显示正确的索引。还在调查。