Monday, April 24, 2017

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 instructs LayoutManager to recalculate layout and often called once new components are added or removed from Container. The revalidate() can also trigger to call the repaint() method to repaint components that 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 the repainting of a component on which it has called.

Another follow-up of this question 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 methods don't do any job and they put a request for EDT, it makes sense of 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 requests 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.

And, if you are new to Java Swing and GUI Programming world then I also recommend you go through Java Swing (GUI) Programming: From Beginner to Expert course by John Purcell on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java Swing and GUI coding online.

Difference between repaint and revalidate method in Java Swing

That's all on the difference between repaint and revalidate method of Swing. It's worth preparing questions 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 Swing (GUI) Programming: From Beginner to Expert
Mastering Java Swing - Part 1
The Complete Java MasterClass


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 a 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 suggestions, feedback, or question then please drop a comment. If you want to learn more about the Swing framework then Java: The Complete Reference, Ninth Edition is worth reading. 

No comments :

Post a Comment