[英]JList throws ArrayIndexOutOfBoundsExceptions randomly
我正在尝试异步向JList添加项目,但我经常从另一个线程获取异常,例如:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
有谁知道如何解决这一问题?
(编辑:我回答了这个问题,因为它一直在困扰我,并且没有明确的搜索引擎友好的方式来查找此信息。)
Swing组件不是线程安全的 ,有时可能抛出异常。 特别是JList在清除和添加元素时会抛出ArrayIndexOutOfBounds异常 。
解决此问题的方法以及在Swing中异步运行事物的首选方法是使用invokeLater
方法 。 它确保在所有其他请求时完成异步调用。
使用SwingWorker
(实现Runnable
)的示例:
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void> () {
@Override
protected Void doInBackground() throws Exception {
Collection<Object> objects = doSomethingIntense();
this.myJList.clear();
for(Object o : objects) {
this.myJList.addElement(o);
}
return null;
}
}
// This WILL THROW EXCEPTIONS because a new thread will start and meddle
// with your JList when Swing is still drawing the component
//
// ExecutorService executor = Executors.newSingleThreadExecutor();
// executor.execute(worker);
// The SwingWorker will be executed when Swing is done doing its stuff.
java.awt.EventQueue.invokeLater(worker);
当然,您不需要使用SwingWorker
因为您可以像这样实现Runnable
:
// This is actually a cool one-liner:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Collection<Object> objects = doSomethingIntense();
this.myJList.clear();
for(Object o : objects) {
this.myJList.addElement(o);
}
}
});
模型接口不是线程安全的。 您只能在EDT中修改模型。
它不是线程安全的,因为它与内容分开询问大小。
你是否可以从另一个线程修改它? 也许可以在执行期望内容保持相同大小的JList
(或相关)方法的同一线程中修改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.