Difference between paint, paintComponent and paintComponents in Swing

Abhishek Choudhary picture Abhishek Choudhary · Feb 22, 2012 · Viewed 31k times · Source

What is the actual difference between paint(), paintComponent() and paintComponents() in Java Swing?

I tried to understand what explained in Oracle docs but I am not clear.

Answer

Andrew Thompson picture Andrew Thompson · Feb 22, 2012
  • AWT, override paint().
  • Swing top-level container (e.g.s are JFrame, JWindow, JDialog, JApplet ..), override paint(). But there are a number of good reasons not to paint in a TLC. A subject for a separate question, perhaps.
  • The rest of Swing (any component that derives from JComponent), override paintComponent().
  • Neither override nor explicitly call paintComponents(), leave it to the API to call it when needed.

Be sure to also use @Override notation whenever overriding a method.

Doing so would hint at the problem of trying to override paintComponent(..) in a JFrame (it has no such method), which is quite common to see.