When does one need to call revalidate() on a swing

2019-01-12 03:16发布

问题:

When dealing with swing components, and when changing the components during runtime, one (often) needs to call the revalidate() method on the components in order for them to refresh.

What are the rules that determine whether or not one needs to revalidate a component? Does swing automatically call revalidate() when some certain properties on a component changes, and for some other properties not?

回答1:

The basic rule is: never - swing internals will take care of it. The basic exception to the rule is adding/removing components at runtime or changes which effect layout in ways the system can't know or for some reason doesn't want to react to.

While the exception is not very clearcut, it's infrequency in "normal" application development is: if there's a need to "often" call it there's a high probability something wrong with the code, f.i. a custom model implementation not notifying its listeners.