marioosh marioosh - 24 days ago 5
Java Question

How to disable children, grandchildren... of control in SWT?

Is simple way to disable all children, grandchildren, great-grandchildren and so on of Control ? Using code below i can disable all children on composite. Need to loop on grandchildren, great-grandchildren etc ??

for(Control c: composite.getChildren()) {
c.setEnabled(false);
}

Answer

You'll have to set each of them enabled/disabled. You can have a simple static helper method:

public static void setEnabledRecursive(final Composite composite, final boolean enabled)
{
    Check.notNull(composite, "composite"); //$NON-NLS-1$

    Control[] children = composite.getChildren();

    for (int i = 0; i < children.length; i++)
    {
        if (children[i] instanceof Composite)
        {
            setEnabledRecursive((Composite) children[i], enabled);
        }
        else
        {
            children[i].setEnabled(enabled);
        }
    }

    composite.setEnabled(enabled);
}