3

I'm trying to add items to a JList asynchronously but I am regularly getting exceptions from another thread, such as:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8

Does anyone know how to fix this?

(EDIT: I answered this question because it's been bugging me and there is no clear search engine friendly way of finding this info.)

6
  • Give more information on how exactly you add items to this list. Do you use your own model?
    – Gnoupi
    Aug 9, 2010 at 13:23
  • Ah. I was going to answer this myself because I've been at this problem for several hours, looking it up on (insert your favorite search engine here).
    – Spoike
    Aug 9, 2010 at 13:37
  • @spoike - in this case, I would recommend you tell that in the question already, or type the answer already on side, to avoid a moment of people rushing to answer a question you will answer fully anyway.
    – Gnoupi
    Aug 9, 2010 at 13:49
  • @Gnoupi: Sorry. ;) I'll add in a blurb about this.
    – Spoike
    Aug 9, 2010 at 14:03
  • The article "Concurrency in Swing" addresses this in the section on "Initial Threads". You may want to add the link to your question download.oracle.com/javase/tutorial/uiswing/concurrency/…
    – trashgod
    Aug 9, 2010 at 15:05

3 Answers 3

8

Swing components are NOT thread-safe and may sometimes throw exceptions. JList in particular will throw ArrayIndexOutOfBounds exceptions when clearing and adding elements.

The work-around for this, and the preferred way to run things asynchronously in Swing, is to use the invokeLater method. It ensures that the asynchronous call is done when all other requests.

Example using SwingWorker (which implements 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);

Of course you don't need to use a SwingWorker as you can just implement a Runnable instead like this:

// 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);
        }
    }
});
3

The model interface is not thread safe. You can only modify the model in EDT.

It is not thread safe because it asks the size separately from the contents.

0

Are you perhaps modifying it from another thread? Could perhaps be modifying it in the same thread during execution of a JList (or related) method that expects the contents to remain the same size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.