TU_HEO DAKAI
TU_HEO DAKAI

Reputation: 2237

What is valid container?

What does "valid" mean in this context?
http://docs.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html

A container can be valid (namely, isValid() returns true) or invalid. For a container to be valid, all the container's children must be laid out already and must all be valid also. The Container.validate method can be used to validate an invalid container. This method triggers the layout for the container and all the child containers down the component hierarchy and marks this container as valid.

Upvotes: 1

Views: 743

Answers (3)

krystan honour
krystan honour

Reputation: 6793

isValid() 

Indicates that the container hierarchy has been laid out correctly as is currently defined by the layout manager (for example BorderLayout), for example if you removed a JButton from a JPanel at runtime then you should call validate() or revalidate() This tells the Swing layout manager to recalculate the layout for the current container and triggers a recursive action down the tree.

Just a note the revalidate() is the same as calling invalidate() then validate(). These operations for nested revalidate() can be expensive.

Upvotes: 2

nIcE cOw
nIcE cOw

Reputation: 24626

In Swing when you create Component, it is not valid i.e. it's valid property is false. A component is said to be valid, when it's width, height, location and stuff has been determined. This is usually done by calling their validate() method, directly or indirectly. When we call validate() on containers, it will validate the container (if it is invalid) by calling its doLayout() method, which typically will invoke the LayoutManager. Now each child placed on this container will be validated recursively, so that the entire tree will be laid out and will become valid.

Upvotes: 2

Lajos Arpad
Lajos Arpad

Reputation: 76591

"Valid" is an attribute of a container. A container can be valid or invalid. If C1 and C2 are containers and C2 is inside C1 and C2 is invalid, C1 is invalid too. If you call C1.validate(), C2 will become valid too. Note that C1 is not valid if C2 is not laid out already.

Upvotes: 2

Related Questions