aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Saving Swing Actions for enabling/disabling GUI controls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Saving Swing Actions for enabling/disabling GUI controls" Watch "Saving Swing Actions for enabling/disabling GUI controls" New topic
Author

Saving Swing Actions for enabling/disabling GUI controls

Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 100
Dear all,

Currently I am working on one of my first larger Swing applications. I build the application around the MVC architecture using a view-independent Controller and, of course, a Swing GUI, holding a reference to the Controller.

I implement most of the button actions in separate classes implementing the Action interface:



The CustomAction then calls a business method in its actionPErformed method.

Now I want to enable/disable those actions (and grey out their controls) dependent of the action is valid in a given situation. But what is a good way of saving all ACtions and getting access to them ?
I have thought of the following possibilities:
- Creating a ActionFactory class that creates all actions in its constructor and implements a getter method for each CustomAction
- Saving all Actions in a Map and use a String key for each CustomAction (but then CodeCompletion won't help me to get a given Action )

Are there better options available ? I hope for some good hints...
Greetz, Klaas


"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
It's common for there to be several ways of accessing the same command, so having some kind of Action lookup component is a must in any complex Swing application.

Saving all Actions in a Map and use a String key for each CustomAction (but then CodeCompletion won't help me to get a given Action )

I've recently been writing a program where I do something similar but still get the benefits of static type checking (with generics), autocompletion, automated refactorings, and all the usual stuff. Instead of

Action getAction(String key);

I use something similar to this:

Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 100
Hi, thanx 4 your reaction !
I have yet no experience with the new 5.0 static type checking features, but I think the key of your story is to use the class of the custom action as a lookup key:



Another issue is where to store this Map. A logic place is my SwingView class, but how to obtain a reference to the Map in all panels ? I use separate classes for all my panels, but then I must pass the Map to the constructors of all these panels... I am not sure if that is the right way...
An additional problem is that some of the Actions need a callback reference to the corresponding panels to obtain information from the controls. ..
Still thinking of thatm any more hints ? :-)
Grtz, Klaas
[ August 11, 2005: Message edited by: Klaas van Gelder ]
 
jQuery in Action, 2nd edition
 
subject: Saving Swing Actions for enabling/disabling GUI controls