How do I recursively disable all of my components in a JPanel?
Asked
Active
Viewed 1,448 times
1 Answers
7
void setEnabled(Component component, boolean enabled) {
component.setEnabled(enabled);
if (component instanceof Container) {
for (Component child : ((Container) component).getComponents()) {
setEnabled(child, enabled);
}
}
}
Be aware that the previous enabled/disabled state of each component will be lost, unless you keep track of it somewhere else.

VGR
- 40,506
- 4
- 48
- 63
-
This is a good (in a relative sense because we still use instanceof...) function. – sdasdadas Dec 17 '12 at 19:12