Disabling group of components

Posted on 1998-09-17
Last Modified: 2008-03-17
I have a group of Swing components (text fileds, combo boxes, buttons) which have to be disabled when certain event takes place. I wonder whether there is more elegant way of doing that than calling setEnabled(false) on each of them individually.
Question by:msmolyak
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 1224477
What you could do is to create an Action class that disables or enables all of the components at once, Your ActionAdapter would take care of it for you. I believe that this is discauused in the Javadocs for Action.class

Expert Comment

ID: 1224478
Just in case that wasn't clear, I meant:

Expert Comment

ID: 1224479
If the components to enable/disable are in the same container, you could replace it by a
more clever one:

public class CleverPanel extends JPanel { // for example

   public void setEnabled(boolean b) {
       for (int i=0; i< getComponentCount(); i++) {
            Component c = getComponent(i);


Like this, you only need to invoke setEnabled() on the container to have all the components
inside enabled/disabled.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 1224480
mjenkings, thank you for the proposed answer. Your solution is a good one, however I decided to go with the fontaine's proposal for the following reasons:

1. All the components I need to enable/disable are indeed inside a panel.
2. fontaine's soultion does not assume that the components are ActionListeners or that they can be creates based on an Action.
3. In this solution code changes are less substantial, they affect only the panel and not any of the subcomponents.
4. Enabling and disabling is easier, I simply have to call setEnabled() on a container rather than deal with events (the event which causes the components to be enabled/disabled is not an ActionEvent but a list selection event).

Bertrand, the points are yours!

Author Comment

ID: 1224481
Sorry, mjenkins, I misspelled you name.


Expert Comment

ID: 1224482
another crude and wild idea is to use the glasspane feature of swing  is disable a group of components.

The idea is to set your own custom glass pane and position and resize it to exactly take the
place of the container. (when ever the Jpanel which needs to be disabled changes its size and position), move the glass pane to that place and resize it too.

see this question of week from site

Question: How do you ignore user input in a Swing application?

              The user has just pressed a button in an application and the operation will
              take some time. You want to put the application in a busy state. How do you
              get the application to ignore user input?


              setEnabled(false) on the frame should work. Unfortunately if you call
              Frame.setEnabled(false), the interface goes blank. To block input without
              hiding the interface, try using a "glasspane". You can set the glasspane visible
              to intercept events before they reach the underlying components.

              Check out:
              which has examples of glasspane usage.

see this example



Accepted Solution

fontaine earned 50 total points
ID: 1224483
If I understand well, I can lock the question...

Author Comment

ID: 1224484
Vijay, It is an interesting suggestions to use a glasspane. However the disadvantages of that approach are (correct me if I am wrong): it looks like it is more difficult to implement and (the main one) even though it does shield the components from the mouse events it does not make them look disabled. That the glasspane does not make it obvious to the user that this button and this text field are disabled. If you disable the components, the changes in background and font color do make it obvious.

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 71
Java syntax, or is it Selenium 6 64
Java 8 to Java 6 8 44
Website checklist for browser compatibility? 2 57
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question