在ObservableList上调用clear()会导致IndexOutOfBoundsException

编程入门 行业动态 更新时间:2024-10-19 12:46:06
本文介绍了在ObservableList上调用clear()会导致IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有observablelist的ComboBox,当用户输入字符或进行选择时,它会更新。当我从ComboBox中选择一个项目并调用我的侦听器事件然后从ComboBox的ObservableList调用clear()方法时,我遇到了问题。

I have a ComboBox with an observablelist which is updated as the user types in characters or makes a selection. The issue I am having is caused when I select an item from the ComboBox and my listener event is called which then calls the clear() method from the ComboBox's ObservableList.

完整代码

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。

Now when I get the error, the ObservableList appears as it should, but I still get this exception. Trying to debug this caused my IDE to freeze after the call to setAll which runs the clear() and I had to kill the IDE via terminal.

如果我替换setAll使用addAll不清除()我没有抛出任何异常,我的列表更新了重新添加的点击项目。

If I replace the setAll with addAll which does not clear() I get no exceptions thrown and my list is updated with the clicked item re-added.

我无法捕获异常我希望看到堆栈跟踪但是想要尝试它的监听器。

I couldn't catch the exception in the Listener which I expected from seeing the stack trace but wanted to try it anyway.

这是堆栈跟踪。

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$new$178(ListViewBehavior.java:264) at com.sun.javafx.scene.control.behavior.ListViewBehavior$$Lambda$321/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$1.handle(BehaviorSkinBase.java:95) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.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.access$1500(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$handleMouseEvent$347(GlassViewEventHandler.java:385) at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$169/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$48(GtkApplication.java:139) at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1091223379.run(Unknown Source) at java.lang.Thread.run(Thread.java:745)

推荐答案

这是一个黑客攻击,但

Platform.runLater(() -> { ObservableList<String> comboList = foodSearch.getItems(); comboList.setAll(foodList); }

似乎解决了这个问题。我认为问题是您在选择模型更改 selectedItems 列表时修改列表,这违反了在通知期间不更改可观察列表的规则。使用 Platform.runLater(...)会延迟修改,直到当前事件完全处理完毕为止。

seems to fix it. I think the issue is that you are modifying the list while the selection model is changing the selectedItems list, which violates a rule on not changing an observable list during notifications. Using the Platform.runLater(...) delays your modification until after the current event has been fully handled.

更多推荐

在ObservableList上调用clear()会导致IndexOutOfBoundsException

本文发布于:2023-11-09 21:00:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ObservableList   clear   IndexOutOfBoundsException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!