Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving Swing Actions for enabling/disabling GUI controls

 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic