How to affect all children by just modifying the Parent in Netbeans?

Viewed 86

I have a jPanel panelA which contains jButtons, jLabels and any other stuff. My question is, if I can set all those components inside panelA as children, how can I make a method like

panelA.setEnable(false);

that makes all children disabled as well, without having to list each and everyone of them?

Is there any way in which this can be generalized to other methods in java.Swing like panelA.setVisible(false);?

1 Answers

there is no predefined function, but you can creat one like following :

void setPanelEnabled(JPanel panel, Boolean isEnabled) {
panel.setEnabled(isEnabled);
Component[] components = panel.getComponents();
for (Component component : components) {
    if (component instanceof JPanel) {
        setPanelEnabled((JPanel) component, isEnabled);
    }
    component.setEnabled(isEnabled);
  }
}
Related