title: Custom JComponent Not Being Added to JPanel
body: <p>I'm having a bit of a stickler with what I thought was quite a simple problem.</p>
  
  <p>I have a custom JComponent <code>Ball</code> that is nothing more than a simple oval. I'm trying to add the ball to a JPanel which I have set as the JFrame's default content pane (via frame.setContentPane(mainPanel)). Along with the ball, I have two other components of class <code>Paddle</code>, another custom JComponent I have created. Oddly, the paddles can be added just fine.</p>
  
  <p>The panel in question uses, on purpose, absolute layout to position the elements, since this was supposed to be a simple recreation of the Pong game.</p>
  
  <p>I ran a simple test after adding the ball and both paddles to the JPanel, and found that the array of Components returned only listed two: the paddles. The full code adding all of them and initializing the JPanel and JFrame is as follows:</p>
  
  <pre><code>public Pong()
  {
      frame = new JFrame("Pong");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      mainPanel = new JPanel(null);
  
      mainPanel.setBackground(Color.black);
      mainPanel.setPreferredSize(new Dimension(500, 500));
  
      player = new Paddle(20, 100);
      enemy = new Paddle(20, 100);
      ball = new Ball(5, Color.GREEN);
  
      mainPanel.add(player);
      mainPanel.add(enemy);
      mainPanel.add(ball);
      enemy.setColor(Color.RED);
  
      frame.setContentPane(mainPanel);
      frame.pack();
  
      Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
      int x = screen.width/2 - frame.getWidth()/2;
      int y = screen.height/2 - frame.getHeight()/2;
      frame.setLocation(x, y);
  
      mainPanel.addKeyListener(mc);
  
      frame.setVisible(true);
  
      mainPanel.repaint();
  
      x = mainPanel.getWidth()/2 - ball.getWidth()/2;
      y = mainPanel.getHeight()/2 - ball.getHeight()/2;
      System.out.println("X: " + x + ", Y: " + y);
      ball.setLocation(x, y);
  
      x = 0;
      y = mainPanel.getHeight()/2 - player.getHeight()/2;
      System.out.println("X: " + x + ", Y: " + y);
      player.setLocation(x, y);
  
      x = mainPanel.getWidth() - enemy.getWidth();
      y = mainPanel.getHeight()/2 - enemy.getHeight()/2;
      System.out.println("X: " + x + ", Y: " + y);
      enemy.setLocation(x, y);
  
      System.out.println(Arrays.deepToString(mainPanel.getComponents()));
  
      mainPanel.repaint();
  
      frame.addMouseListener(new MouseAdapter()
      {
          @Override
          public void mousePressed(MouseEvent e)
          {
              mainPanel.requestFocus();
          }
      });
  
      dm.start();
      mainPanel.requestFocus();
  }
  </code></pre>
  
  <p>The code for the ball is as follows [<b>Note:</b> The ball is private because it is an inner class of class <code>Pong</code>, which does not extend anything]:</p>
  
  <pre><code>private class Ball extends JComponent
  {
      private int radius;
      private Color color;
  
      public Ball(int size, Color c)
      {
          if(size &gt; 0)
              radius = size;
          else
              radius = 1;
          this.setPreferredSize(new Dimension(radius, radius));
          color = c;
      }
  
      public Ball(int size)
      {
          this(size, Color.RED);
      }
  
      //Getters and Setters...
  
      @Override
      public void paintComponent(Graphics g)
      {
          System.out.println("Radius: " + radius);
          g.setColor(color);
          g.fillOval(0, 0, radius, radius);
      }
  }
  </code></pre>
  
  <p>This isn't the first time I've encountered this problem when working on projects, and it always seems to occur when I'm using custom JComponents along with absolute positioning on a JPanel, although in still other projects, I don't seem to have a problem with this. It's probably something very simple that I'm overlooking, but I can't think of what it is, for the life of me. Any answers to this would be greatly appreciated.</p>
  
  <p>If absolutely necessary, <a href="http://pastebin.com/BKervdxt" rel="nofollow">all the lines of code for this project can be found here</a> (Just over 300 lines total). </p>
  
  <p>Thanks in advance for any help! </p>