title: JLabels in GridLayout are Not Always Painted
body: <p>I am creating a basic Tic-Tac-Toe application in Java Swing, and for this purpose, I have started investigating drawing. However, I have encountered an issue when a subclass of <code>JPanel</code> contains more than one instance of a subclass of <code>JLabel</code>, which overrides the <code>paintComponent(Graphic)</code> method, in a <code>GridLayout</code> array format embedded in itself. The issue is that <strong>only the first element in this array is painted</strong>.</p>
  
  <p>An example for the customized subclass of <code>JLabel</code>:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  
  import javax.swing.JLabel;
  
  @SuppressWarnings("serial")
  public class DrawLabel extends JLabel {
  
      private boolean hasPaint;
  
      public DrawLabel() {
          super();
          this.hasPaint = false;
      }
  
      public void draw() {
          hasPaint = true;
          repaint();
      }
  
      public void clear() {
          hasPaint = false;
          repaint();
      }
  
      @Override
      public Dimension getPreferredSize() {
          return new Dimension(200, 200);
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          Graphics2D g2 = (Graphics2D) g;
  
          if (hasPaint) {
              g2.drawOval(getX(), getY(), getWidth(), getHeight());
          } else {
              g2.clearRect(getX(), getY(), getWidth(), getHeight());
          }
      }
  }
  </code></pre>
  
  <p>An example for the customized subclass of <code>JPanel</code>:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.GridLayout;
  
  import javax.swing.JPanel;
  
  @SuppressWarnings("serial")
  public class DrawPanel extends JPanel {
  
      private Dimension size;
      private DrawLabel[][] fields;
  
      public DrawPanel(Dimension d) {
          super();
          this.size = d;
          this.fields = new DrawLabel[size.height][size.width];
  
          this.setLayout(new GridLayout(size.height, size.width));
  
          for (int i = 0; i &lt; size.height; i++) {
              for (int j = 0; j &lt; size.width; j++) {
                  this.fields[i][j] = new DrawLabel();
                  this.add(fields[i][j]);
              }
          }
      }
  
      public void draw(int row, int col) {
          fields[row][col].draw();
      }
  
      public void clear() {
          for (int i = 0; i &lt; size.height; i++) {
              for (int j = 0; j &lt; size.width; j++) {
                  fields[i][j].clear();
              }
          }
      }
  }
  </code></pre>
  
  <p>An example of an issue-provoking scenario:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Dimension;
  
  import javax.swing.JFrame;
  
  public class Driver {
  
      public static void main(String[] args) {
          DrawPanel board = new DrawPanel(new Dimension(2, 1));
  
          JFrame canvas = new JFrame();
          canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          canvas.add(board, BorderLayout.CENTER);
          canvas.pack();
  
          canvas.setLocationRelativeTo(null);
          canvas.setVisible(true);
  
          board.draw(0, 0); // This gets painted.
          board.draw(0, 1); // This does not get painted!
      }
  
  }
  </code></pre>
  
  <p>Curiously, if the <code>DrawPanel.draw(int, int)</code> is changed to set the text of the element rather than draw upon it, only the second element is updated, while the first is not, which is the exact opposite of the original problem.</p>
  
  <p>I have tried to look up other issues and questions related to painting of subcomponents, but I have yet to find an issue like this one, where it seems that every instance beyond the first in the <code>GridLayout</code> are not drawable in the same way. What could be wrong?</p>
  
  <p>Thank you for your time and effort!</p>