Difference between repaint and revalidate method in Java Swing

What is the difference between revalidate() and repaint() in Java Swing is one of the popular Swing Interview Questions especially on Investment banks? Though both methods sound similar, there is a subtle difference between them. the revalidate() method instruct LayoutManager to recalculate layout and often called once new components are added or removed from Container. The revalidate() can also trigger to call to the repaint() method to repaint components which have changed i.e. width, height or any other visible property, which can affect layout. On the other hand repaint() method puts a paint request in AWT thread, for repainting of a component on which it has called.

Another follow-up of this questions is regarding thread-safety, What will happen if you call repaint() or revalidate() method from any thread, other than EDT? Many programmers confuse here because they know Swing is single threaded and properties of the component should not be altered from a thread other than Event Dispatcher thread, but interestingly repaint() and revalidate() method are thread-safe.

Since these method doesn't do any job and they put a request for EDT, it makes sense them being thread-safe. By the way, there is nothing wrong calling both revalidate() and repaint() after adding new components into the container, Since multiple print request can be combined by EDT thread.


Calling repaint() after revalidate() ensures that your layout and screen are up to date. So if you are using any container, like JFrame or JPanel and keep adding and removing components, always call revalidate() and repaint() to refresh GUI.

Difference between repaint and revalidate method in Java Swing

That's all on the difference between repaint and revalidate method of Swing. It's a worth preparing question before appearing into any Java Swing Interview. This question is not as popular as the difference between invokeAndWait() and invokeLater(), but nevertheless a good question and as an experienced Java developer, you should know the difference between these two methods.

Further Reading
Java Fundamentals, Part 1 and 2
Mastering Java Swing - Part 1
Java Swing Development Using NetBeans


Other Java Swing Tutorials and Interview Questions You may find useful
  • Is Swing Components Thread-Safe in Java? (answer)
  • Difference between invokeAndWait() and invokeLater() in Java? (answer)
  • Top 10 AWT Swing Interview Questions with Answers (answer)
  • How to close a Swing Application in Java? (answer)
  • How to use List in Java Swing Application (tutorial)
  • What is blocking method in Java? (answer)
  • 12 Java Books for Experienced Developers (books)
  • Best books and Mock Exam for OCAJP 8 Certification (guide)

Thanks for reading this article, if you like this article and interview question then please share with your friends and colleagues. If you have any suggestion, feedback or question then please drop a comment. If you want to learn more about Swing framework then Java: The Complete Reference, Ninth Edition is worth reading. 

No comments :

Post a Comment