tags:

views:

30

answers:

2

I have a specific requirement that all children of a particular JComponent have double buffering turned off. I can recurse through it easily enough and disable them when needed, but I'd like to detect the addition of new children components regardless of their position in the hierarchy and disable it then.

I've taken a look at addHierarchyChangeListener but it seems to only detect changes in parents, not children.

Can someone point me down the right path?

+1  A: 

You are looking for a ContainerListener. Here is Sun's demo.

Yishai
+1  A: 

I think you need a RecursiveContainerListener that could execute a common operation whenever any component added or removed in a given parent component. Please find the code for it here(RecursiveContainerListener.java). It does exactly what you want. All you need is to change/override handleAdd and handleRemove methods to turn of double buffering.

And finally you simply add it to your parent:

parentComponent.add(new RecursiveContainerListener());

No need to explicitly add listener to each child :)

Suraj Chandran
Um, RecursiveContainerListener doesn't support that constructor, and ultimately, it is adding a ContainerListener to all components. Though it is an interesting bit of code.
Allain Lalonde
You have to modify it a bit to suit your needs.You can completely remove the constructor and member field listener and keep everything else same. And finally override the handleAdd/handleRemove methods :) Anyway the intention was to give another approach of doing thing.
Suraj Chandran