Difference between paint() and paintcomponent()?

Boogley Beegly picture Boogley Beegly · Feb 27, 2013 · Viewed 13.9k times · Source

I have tried tutorials on this but I still don't quite understand it. Basically my question is which method is better and why? Should I use paint or paintComponent?

Please try to keep the answer simple, thanks.

Answer

christianhs picture christianhs · Feb 27, 2013

Quoting from documentation of paint() method

This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. ... A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.

It looks like the paint() method actually draws the component, including the border and children. If you only want to customize the component's appearance excluding the border and children, you use paintComponent().

http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paint(java.awt.Graphics)