Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Friday, July 30, 2021

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 the 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.

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 in 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.

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 the interview question then please share it 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