Swing components are not thread safe and can sometimes throw exceptions. JList, in particular, will throw ArrayIndexOutOfBounds exceptions when clearing and adding items.
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;
}
}
java.awt.EventQueue.invokeLater(worker);
, SwingWorker, Runnable, :
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Collection<Object> objects = doSomethingIntense();
this.myJList.clear();
for(Object o : objects) {
this.myJList.addElement(o);
}
}
});