views:

108

answers:

1

How do I fix this code? i don't know what this error means... I heard that it comes from having elements of a list removed during a for each loop, but I don't see anything tha I'm removing...

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        for(Layer e : layerList)
            e.drawLayer(g2);
    }

The jcomponent has a list of objects called layers that it passes Graphics to so the layers can paint themselves. I never remove any of the layers or anything, so I'm lost. Help?

A: 

If you have more then one thread working that could be working with the layerlist, you should consider using a synchronize block as an example below. That will help prevent this problem or you could consider having the layerList be synchronized but with out more info of the program and the thread structure it is hard to tell you what is best. Check out this synchronized list as an option instead of the synchronized block.

 synchronized( layerList ) 
 {
    for(Layer e : layerList)
        e.drawLayer(g2);
 }
Jeff Beck