Java Swing - How to disable a JPanel?

Yatendra Goel picture Yatendra Goel · Apr 26, 2010 · Viewed 35.9k times · Source

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.

Answer

ZeissS picture ZeissS · Apr 26, 2010

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