title: paintComponent ConcurrentModificationException
body: <p>I'm writing an application in swing, and I have a component that draws objects that are being dragged. Here's a small section of the code:</p>
  
  <pre><code>public class DragPanel extends JComponent{
  
      private final int CARD_STACK_SPACING;
      private int x, y;
      private List&lt;Card&gt; cards;
  
      public DragPanel(int cardStackSpacing) {
          x = -1;
          y = -1;
          cards = null;
          CARD_STACK_SPACING = cardStackSpacing;
      }
  
      public void drag(int x, int y, Card card) {
          this.x = x;
          this.y = y;
          cards = new ArrayList&lt;Card&gt;();
          cards.add(card);
          repaint();
      }
      public void dragCards(int x, int y, List&lt;Card&gt; cards) {
          this.x = x;
          this.y = y;
          this.cards = cards;
          repaint();
      }
      public void stopDragging() {
          x = -1;
          y = -1;
          cards = null;
          repaint();
      }
  
      @Override
      public void paintComponent(Graphics g) {
          if (x &gt; 0 &amp;&amp; y &gt; 0) {
              Graphics2D g2d = (Graphics2D) g;
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              int adjustedY = y;
              for (int i = 0; i &lt; cards.size(); ++i) {
                  CardImager.drawCard(cards.get(i), g2d, x, adjustedY);
                  adjustedY += CARD_STACK_SPACING;
              }
          }
      }
  }
  </code></pre>
  
  <p>Now, when I run this code, I get a <code>ConcurrentModificationException</code>, which I think is because I have a for loop in <code>paintComponent</code> that iterates over the list of cards, and I also have methods that set the list of cards to something different.</p>
  
  <p>My question is, what can I do to make sure this doesn't happen?</p>
  
  <p><strong>Edit:</strong> here's some more information, when the mouse is dragged, this method is called, which calls the dragCards method from dragPanel above.</p>
  
  <pre><code>@Override
  public void mouseDragged(MouseEvent e) {
      firstClick = 0;
      if (!isDragging &amp;&amp;
              !isDraggingStack &amp;&amp;
              e.getSource() instanceof ResultDisplay &amp;&amp; 
              searchPanel.getCard(e.getX(), e.getY()) != null) {
  
          isDragging = true;
          draggingCard = searchPanel.getCard(e.getX(), e.getY());
          offsets = searchPanel.getOffsets(e.getX(), e.getY());
          dragPanel.drag(e.getX() + offsets[0], e.getY() + offsets[1], draggingCard);
  
      } else if (!isDragging &amp;&amp;
              !isDraggingStack &amp;&amp;
              e.getSource() instanceof DeckBuilderPanel &amp;&amp;
              deckBuilderPanel.getCard(e.getX(), e.getY()) != null) {
  
          isDraggingStack = true;
          offsets = deckBuilderPanel.getOffsets(e.getX(), e.getY());
          draggingCardStack = deckBuilderPanel.removeCardStack(e.getX(), e.getY());
          dragPanel.dragCards(e.getX() + offsets[0], e.getY() + offsets[1] + searchPanel.getHeight(), draggingCardStack);
          System.out.println(offsets[0] + " " + offsets[1]);
      }
      if (isDragging &amp;&amp; !isDraggingStack) {
          dragPanel.drag(e.getX() + offsets[0], e.getY() + offsets[1], draggingCard);
      } else if (!isDragging &amp;&amp; isDraggingStack) {
          dragPanel.dragCards(e.getX() + offsets[0], e.getY() + offsets[1] + searchPanel.getHeight(), draggingCardStack);
      }
  }
  </code></pre>