在 ObservableList 上调用 clear() 会导致 IndexOutOfBoundsException
Calling clear() on ObservableList causes IndexOutOfBoundsException
我有一个带有可观察列表的 ComboBox,当用户输入字符或创建 selection 时,它会更新。我遇到的问题是当我 select ComboBox 中的一个项目和我的侦听器事件被调用时引起的,然后调用 ComboBox 的 ObservableList 中的 clear() 方法。
public void suggestItem(ActionEvent ev){
String currentInput = foodSearch.getEditor().getText();
if(currentInput.length() > 4){
DatabaseCommunicator.openConnection();
// Returns a list no greater than size 5 of possible food items
ArrayList<String> foodList = DatabaseCommunicator.findSimilarFoods(currentInput);
ObservableList<String> comboList = foodSearch.getItems();
comboList.setAll(foodList);
DatabaseCommunicator.closeConnection();
}
}
现在,当我收到错误时,ObservableList 会正常显示,但我仍然收到此异常。尝试调试它导致我的 IDE 在调用运行 clear() 的 setAll 后冻结,我不得不通过终端终止 IDE。
如果我用不清除的 addAll 替换 setAll(),我不会抛出任何异常,并且我的列表会更新并重新添加单击的项目。
我无法在 Listener 中捕获异常,这是我希望从堆栈跟踪中看到的异常,但无论如何都想尝试一下。
这是堆栈跟踪。
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)
at javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:242)
at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new8(ListViewBehavior.java:264)
at com.sun.javafx.scene.control.behavior.ListViewBehavior$Lambda1/1588822558.onChanged(Unknown Source)
at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:75)
at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:331)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1385)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:260)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:224)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3719)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3447)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1723)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2456)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent7(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$Lambda9/414422402.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:387)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:549)
at com.sun.glass.ui.View.notifyMouse(View.java:921)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$Lambda/1091223379.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
这有点像 hack,但是
Platform.runLater(() -> {
ObservableList<String> comboList = foodSearch.getItems();
comboList.setAll(foodList);
}
似乎已解决。我认为问题在于您在选择模型更改 selectedItems
列表时修改列表,这违反了在通知期间不更改可观察列表的规则。使用 Platform.runLater(...)
将您的修改延迟到当前事件完全处理之后。
我有一个带有可观察列表的 ComboBox,当用户输入字符或创建 selection 时,它会更新。我遇到的问题是当我 select ComboBox 中的一个项目和我的侦听器事件被调用时引起的,然后调用 ComboBox 的 ObservableList 中的 clear() 方法。
public void suggestItem(ActionEvent ev){
String currentInput = foodSearch.getEditor().getText();
if(currentInput.length() > 4){
DatabaseCommunicator.openConnection();
// Returns a list no greater than size 5 of possible food items
ArrayList<String> foodList = DatabaseCommunicator.findSimilarFoods(currentInput);
ObservableList<String> comboList = foodSearch.getItems();
comboList.setAll(foodList);
DatabaseCommunicator.closeConnection();
}
}
现在,当我收到错误时,ObservableList 会正常显示,但我仍然收到此异常。尝试调试它导致我的 IDE 在调用运行 clear() 的 setAll 后冻结,我不得不通过终端终止 IDE。
如果我用不清除的 addAll 替换 setAll(),我不会抛出任何异常,并且我的列表会更新并重新添加单击的项目。
我无法在 Listener 中捕获异常,这是我希望从堆栈跟踪中看到的异常,但无论如何都想尝试一下。
这是堆栈跟踪。
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)
at javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:242)
at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new8(ListViewBehavior.java:264)
at com.sun.javafx.scene.control.behavior.ListViewBehavior$Lambda1/1588822558.onChanged(Unknown Source)
at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:75)
at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:331)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1385)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:260)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:224)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3719)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3447)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1723)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2456)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent7(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$Lambda9/414422402.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:387)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:549)
at com.sun.glass.ui.View.notifyMouse(View.java:921)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$Lambda/1091223379.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
这有点像 hack,但是
Platform.runLater(() -> {
ObservableList<String> comboList = foodSearch.getItems();
comboList.setAll(foodList);
}
似乎已解决。我认为问题在于您在选择模型更改 selectedItems
列表时修改列表,这违反了在通知期间不更改可观察列表的规则。使用 Platform.runLater(...)
将您的修改延迟到当前事件完全处理之后。