Repaint(), validate(), invalidate() HOW, WHEN AND WHERE?

Repaint() :
Repaint() method just repaints the component. It is the method of Component class. Repaint() causes calls paint() method of a Component if it is a lightweight component. It is better to call the repaint method rather than paint method because painting system calls paint at appropriate time which coalesces multiple calls into one call. Another disadvantage of calling paint rather than repaint is it bypasses the double buffering mechanism.

Before checking validate first we need to check when we say Component is valid what it means? It means that the Component coordinates has been determined and nothing is changed changing these coordinates. When the Component is created the valid property is false so we can say it as invalid as the coordinates hasn’t been determined. Validate() method validates the component by calling doLayout(). Validation is performed on Container and on its child components recursively which results in all the components to be laid out and validated. Validate() method is necessary when we remove the component and add it again.

This method makes components invalid , along with all of its ancestor containers up to the
nearest validate root. Validate root can be any container which doesn’t change the size when their child components change size. This method need to call when components needs to have new layout to be done.

When we call invalidate() it doesn’t automatically calls validate to make Components to be valid again. Revalidates calls invalidate first and then make components valid by calling validate method. Most swing components calls revalidate() method when we change the size of the components and call the repaint method.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


  1. Have you ever thought about including a little bit more than just your articles? I mean, what you say is valuable and all. However just imagine if you added some great graphics or videos to give your posts more, "pop"! Your content is excellent but with pics and clips, this site could certainly be one of the most beneficial in its field. Excellent blog!