What is the difference between repaint() and revalidate() in Swing components?

Sandip Chitale

The following might help -



My understanding or invalidate(), validate(), revalidate(), repaint() is as follows -
  1. invalidate()/validate()/revalidate() only affects the layout. It results in a repaint if the layout actually changes.
  2. 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 invalid. Then call the parent container's validate() method. In the validate() method the parent checks if at least one of its immediate children is invalid. If it finds 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.

  3. 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.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact