简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM