H2ONaCl
H2ONaCl

Reputation: 11279

how do I choose between JComponent.revalidate and Container.validate

I am using WindowBuilder and Swing. I am performing the following on a Swing JPanel:

java.awt.Container.setLayout()
java.awt.Container.removeAll()
java.awt.Container.add()

To finish I invoke:

java.awt.Container.validate() but I noticed that javax.swing.JComponent.revalidate() seems to do the same thing in practice. I have not observed any difference in behaviour. JPanel inherits from both Container and JComponent. How do I choose between them?

Upvotes: 1

Views: 478

Answers (1)

Ingo Kegel
Ingo Kegel

Reputation: 48015

3 important differences:

  • JComponent#revalidate() invalidates the component first which Container#validate() does not do.
  • JComponent#revalidate() does not validate immediately, it adds the component to a list of invalid components and the RepaintManager will validate components in batches. This can improve performance when lots of validations are required.
  • JComponent#revalidate() can be called from any thread, not just the event dispatch thread.

Unless you need an immediate effect (which is only needed in special situations), JComponent#revalidate() is preferrable.

Upvotes: 1

Related Questions