I cannot figure out a way to disable a container AND its children in Swing. Is Swing really missing this basic feature?
If I do setEnabled(false) on a container, its children are still enabled.
My GUI structure is pretty complex, and doing a traversion of all elements below the container is not an option. Neither is a GlassPane on top of the container (the container is not the entire window).
As VonC's answer, there's no simple solution existed. So i recommend you to program with a supporting infrastructure from the start.
A simple infrastructure is likely to be, for example, using delegated listeners that do a "event enabled" check from a super container's flag before actual event-respond:
Or even better, you can use the APT to automatically inject the boilerplate code for you.
This works well all the time. It's the clean way to block both user interaction and programming calls with a single effort. Even though it costs you some codes to support the underlying functionality, you get simplicity, usablity and stability in return.
PS. i'd like to see better solution to this problem.
I would suggest you to write a recursive method which finds all the java.awt.Container instances in your java.awt.Container and sets its components enabled/disabled. This is how I solved such a problem in my extended JFrame class:
This is the code I use. It recursively visits the component tree, maintaining a counter for each component. Only weak references are kept on the components, preventing any memory leak.
You say that traversing all the elements is not an option, but my experience is that this code works well for quite complex GUIs. By the way, if Swing had this feature natively, there would be no other way than traversing the component tree, anyway.
Example usage (parenthesis means disabled) :
Now the code :
JXLayer might be what you're looking for, according to this post:
alt text http://www.java.net/download/javadesktop/blogs/alexfromsun/2007.06.25/LayerDemo.PNG
This is what I came up with.
To add to mmyers's answer, disabling children is not an easy task (see this thread)