tags:

views:

150

answers:

2

There seems to be many methods in Java awt Container class that are related to validate. Apparently they don't do data validation. Is it useful for a Swing developer in any cases? Some of the methods: validate(), invalidate(), validateTree(), isValid() etc.

+5  A: 

Citing the API doc:

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

Michael Borgwardt
You read the RTRM! (Although typically you would use `JComponent.revalidate`.)
Tom Hawtin - tackline
+2  A: 

Validation in a Swing context concerns requesting a component to lay-out its sub-components after one of these is modified.

For example, suppose you implement a custom JDialog with a button "Show Filters". Upon clicking this button, you might want to add an additional "filter" panel to the south of the JDialog. Upon adding the new sub-panel you would be required to call validate() on the JDialog to cause it to lay-out the new panel correctly.

Adamski
+1. Some people also call validate() as a talisman to magically make things work right...so watch for unnecessary calls if you're working in other people's GUI code.
Alex Feinman