tags:

views:

156

answers:

1

Currently, the disable a JPanel including all its components with a single method call, I have to override its disable method as follow :

@Override
public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    Component[] components = getComponents();
    if (components != null && components.length > 0) {
    int count = components.length;
    for (int i = 0; i < count; i++)
        components[i].setEnabled(enabled);
    }
}

Is there any standard way to do so? Instead providing my own implementation?

+1  A: 

Firstly, you can easily simplify that code:

@Override
public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    for (Component component : getComponents())
        component.setEnabled(enabled);
    }
}

This of course destroys any individual enabled state of the components.

Often disabled components look bad, and a glass pane over the top to stop pointer input and perhaps add a more subtle visual effect is the way to go.

You could take a model approach. When creating each component have it delegate the enabled state to a model. Then you only need a single state change to update all the components. This enables you to have arbitrary groups and facilitates more complex logic.

Tom Hawtin - tackline
Not exactly to my question. Is there any standard way to achieve this?
Yan Cheng CHEOK
Why would there be?
Tom Hawtin - tackline