Wednesday, September 01, 2004

Overriding validate() method for Containers.

I found an interesting problem when overriding the validate() method of the JPanel. I wanted to do some operation when the size of the parent changes. But when the program was executed nothing happened. The validate() method was not called. Where was the problem???????

Actually the answer lies in the validateTree() method of Container class . The validate() method is called only for the Containers which are Window otherwise the validateTree() method is called. So the flow is something like this :

validate() method of Container calls the validateTree(), which in turn calls the validate() if Container is Window otherwise validateTree().

So if your Component is Window then override validate() method otherwise override validateTree() method.