I have several JComponent
s 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.
The answer is pretty simple. Use getComponents() to iterate through an array of components added to the JPanel. Find the kind of component you want to remove, using instanceof for example. In my example, I remove any JCheckBoxes added to my JPanel.
just use firstFrame. setVisible(false) on the first frame.
JPanel, a part of the Java Swing package, is a container that can store a group of components.
Both JFrame and JPanel are important as a swing GUI cannot exist without these top-level containers. JFrame and JPanel both provide different methods to perform different GUI related functions.
The panel should have a getComponents()
method which can use in a loop to disable the sub-components without explicitly naming them.
The Disabled Panel provides support for two approaches. One to recursively disable components, the other to "paint" the panel with a disabled look.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With