What is the difference between repaint() and revalidate() in Swing components?
- invalidate()/validate()/revalidate() only affects the layout. It results in a repaint if the layout actually changes.
invalidate()/validate() is the mechanism for batching changes (like
beginPaint, paint, paint, paint, endPaint of windowing SDKs). You mark,
using invalidate(), all the components that might affect the layout as
Then call the parent container's validate() method. In the validate() method
parent checks if at least one of its immediate children is invalid. If
a child marked invalid, it calls layoutComponents() on its layout manager.
If the layoutmanager actually re-lays out the children, then, as a side effect, repaint() will get called.
The thing to note here is that the Container only looks for invalid immediate children.
For precisely this reason sometimes the resulting layout is not what you expected as the parent's parent and children's chidren may not get re-layed out. To work around this issue Swing added the revalidate() method.
- What revalidate() does is basically marks all the container upto the top level (window and its subclasses or applet) as invalid. Then it calls validate() on the top level.