Amit
Amit

Reputation: 34725

Java Swing - How to disable a JPanel?

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.

Upvotes: 19

Views: 42113

Answers (6)

Kalle Richter
Kalle Richter

Reputation: 8728

Disabling should occur recursively:

Queue<Component> queue = new LinkedList<>(Arrays.asList(container.getComponents()));
while(!queue.isEmpty()) {
    Component head = queue.poll();
    head.setEnabled(enable);
    if(head instanceof Container) {
        Container headCast = (Container) head;
        queue.addAll(Arrays.asList(headCast.getComponents()));
    }
}

Upvotes: 0

toto_tico
toto_tico

Reputation: 19027

The following method should be all what you need to add, you can call it with setEnableRec(panel, true) or setEnableRec(panel, false):

private void setEnableRec(Component container, boolean enable){
    container.setEnabled(enable);

    try {
        Component[] components= ((Container) container).getComponents();
        for (int i = 0; i < components.length; i++) {
            setEnableRec(components[i], enable);
        }
    } catch (ClassCastException e) {

    }
}

Upvotes: 0

Joel Christophel
Joel Christophel

Reputation: 2653

The following method uses recursion to achieve this. Pass in any Container, and this method will return a Component array of all of the non-Container components located anywhere "inside" of the Container.

    private Component[] getComponents(Component container) {
        ArrayList<Component> list = null;

        try {
            list = new ArrayList<Component>(Arrays.asList(
                  ((Container) container).getComponents()));
            for (int index = 0; index < list.size(); index++) {
                for (Component currentComponent : getComponents(list.get(index))) {
                    list.add(currentComponent);
                }
            }
        } catch (ClassCastException e) {
            list = new ArrayList<Component>();
        }

        return list.toArray(new Component[list.size()]);
        }
    }

Simply loop through the elements of the returned array and disable the components.

for(Component component : getComponents(container)) {
    component.setEnabled(false);
}

Upvotes: 3

camickr
camickr

Reputation: 324098

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

Upvotes: 3

Istao
Istao

Reputation: 7585

Use the JXLayer, with LockableUI.

Upvotes: 2

ZeissS
ZeissS

Reputation: 12135

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

Upvotes: 25

Related Questions