How do I deactivate all the elements of a jpanel? in java

1

I try it this way:

jPanel.setEnabled(false);

But it does not work.

Is there another option?

    
asked by Pablo Contreras 29.08.2017 в 23:05
source

3 answers

1

You should basically obtain all the components and deactivate them, as shown below:

public void enableComponents(Container container, boolean enable) {
    Component[] components = container.getComponents();
    for (Component component : components) {
        component.setEnabled(enable);
        if (component instanceof Container) {
            enableComponents((Container)component, enable);
        }
    }
}

In the example you can see the Container attribute that would be your JPanel since it extends from the first as seen in the documentation of Container in the AWT documentation .

The complete example can be found at StackOverflow in English .

    
answered by 29.08.2017 / 23:13
source
2

You must block each of the panel elements, for example:

jTextFile1.setEnabled(false);
jTextFile2.setEnabled(false);
    
answered by 29.08.2017 в 23:10
2

With your code what you do is "deactivate" the JPanel itself (which really does not do much).

If you want to deactivate the elements contained in the JPanel , you will have to obtain the list of these elements and go through it, deactivating each element separately.

for (Component component : jPanel.getComponents()) {
   component.setEnabled(false); 
}
    
answered by 29.08.2017 в 23:11