Solved

Disabling group of components

Posted on 1998-09-17
8
201 Views
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.
0
Comment
Question by:msmolyak
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:mjenkins
Comment Utility
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
0
 
LVL 3

Expert Comment

by:mjenkins
Comment Utility
Just in case that wasn't clear, I meant:

com.sun.java.swing.Action
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
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);
            c.setEnabled(b);
       }

       return;
   }
}

Like this, you only need to invoke setEnabled() on the container to have all the components
inside enabled/disabled.
0
 
LVL 5

Author Comment

by:msmolyak
Comment Utility
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!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Author Comment

by:msmolyak
Comment Utility
Sorry, mjenkins, I misspelled you name.

Michael
0
 
LVL 4

Expert Comment

by:evijay
Comment Utility
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.


http://java.sun.com/docs/books/tutorial/ui/swing/rootpane.html#glasspane

see this question of week from developer.javasoft.com 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?

               Answer:

              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:
              http://java.sun.com/docs/books/tutorial/ui/swing/rootpane.html#glasspane
              which has examples of glasspane usage.


see this example

http://java.sun.com/docs/books/tutorial/ui/swing/example-swing/GlassPaneDemo.java

Thanx
vijay

0
 
LVL 5

Accepted Solution

by:
fontaine earned 50 total points
Comment Utility
If I understand well, I can lock the question...
0
 
LVL 5

Author Comment

by:msmolyak
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now