tags:

views:

472

answers:

3

I have several JComponents on a JPanel and I want to disable all of those components when I press a Start button.

At present, I am disabling all of the components explicitly by

component1.setEnabled(false);
:
:

But Is there anyway by which I can disable all of the components at once? I tried to disable the JPanel to which these components are added by

panel.setEnabled(false);

but it didn't work.

+7  A: 

The panel should have a getComponents() method which can use in a loop to disable the sub-components without explicitly naming them.

ZeissS
+1 It is a good idea
Yatendra Goel
+1 I've used this. Warning though, if your sub-components are containers (e.g. boxes used for layout management) then you need to do it recursively. Also, If you have components that get disabled for other reasons then re-enabling will defeat them.
staticman
Agree with staticman, it is a dangerous behaviour and you need care when using it because you will re-enable everything if you don't take some cautions. Perhaps it is a not a problem for your case then you can like this. Or if it is a pb, then you will probably have to memorize the states of the components before de-enabling them in order to reset them in the good state.
Matthieu BROUILLARD
Its a bit tricky, but I wouldn't say dangerous. My normal approach would be to subclass `JPanel` and overwrite the `setEnabled()` method. There I would manually enable/disable the appropriate subcomponents, based on the internal state.
ZeissS
A: 

Use the JXLayer, with LockableUI.

Istao
A: 

The Disabled Panel provides support for two approaches. One to recursively disable components, the other to "paint" the panel with a disabled look.

camickr