Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing - How to disable a JPanel?

Tags:

java

swing

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.

like image 411
Yatendra Avatar asked Apr 26 '10 12:04

Yatendra


People also ask

How do you remove an element from a JPanel?

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.

How do I turn off JFrame?

just use firstFrame. setVisible(false) on the first frame.

Is JPanel part of Swing?

JPanel, a part of the Java Swing package, is a container that can store a group of components.

Do you need a JPanel in JFrame?

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.


2 Answers

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

like image 165
ZeissS Avatar answered Sep 22 '22 14:09

ZeissS


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

like image 22
camickr Avatar answered Sep 22 '22 14:09

camickr