title: Creating a simple custom JComponent in Java?
body: <p>I want to start building my own customized JComponent's for a project at work. I have a simple example below that should just create a ball on the screen. (I found most of it on the internet) but it does provide a decent starting point. My question is why does this code not show the ball in my form? What have I done wrong?</p>
  
  <p>Also what would be all of the basic methods that SHOULD be provided for a custom JComponent?</p>
  
  <p>Code:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.GridBagLayout;
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class testBall {
      public static void main(String[] args) {
          new testBall();
      }
  
      public testBall() {
          JPanel testPane = new JPanel();
          testPane.setBackground(Color.white);
          testPane.setLayout(new GridBagLayout());
          testPane.add(new MyBall(30,30,10));
  
          JFrame frame = new JFrame("Testing");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new BorderLayout());
          frame.add(testPane);
          frame.pack();
          frame.setSize(500, 300); 
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
  }
  
  class MyBall extends JComponent
  {
      private static final long serialVersionUID = 1L;
      public MyBall() { }
      public MyBall(int x, int y, int diameter)
      {
          super();
          this.setLocation(x, y);
          this.setSize(diameter, diameter);
      }
  
      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
          g.setColor(Color.red);
          g.fillOval(0, 0, 100, 100);
      }
  }
  </code></pre>
  
  <p>Where I could find a list of all of the methods that should be overridden in a JComponent class? (I know there are ones that should always be included in JComponent.)</p>
  
  <p>If I make an instance of this component in a class and need to change the color of the circle would I just call there <code>repaint()</code> method from that class?</p>