Get your CodeRanch badge!*
The moose likes Swing / AWT / SWT and the fly likes Separate Actions and GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Separate Actions and GUI" Watch "Separate Actions and GUI" New topic
Author

Separate Actions and GUI

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
Hey guys, I've been battling with this problem for a while now, and I'm wondering what the people here usually do.

I have a GUI, and I want to separate the Actions from it. I'm not quite sure how to go about it.
The actions will be added to a JMenuBar, and some of them to a JToolBar as well. Performing some of these actions may disable/enable other actions, so they need to be accessible in some way. Also, a user might customize which actions appear in the toolbar.

Let's say my GUI (MainForm) is in the package infinity.view. What I'm currently thinking about is accessing my actions through a static factory (MainFormActionFactory) in infinity.view.event. The factory will create/return singleton instances of these actions using either enum constants or static final Strings or something as an argument.
Amongst others, one of the things that troubles me about this setup is that MainFormActionFactory will have to be public, as well as its factory method. Arguably, this is exactly what you want, in a setup that separates the controller from the view.
Also, should I make a factory like this for every form/dialog that I make?

I would like to know your opinions on the matter, or similar situations.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
I also just bumped into an interesting looking class named ActionMap. What is the purpose of this class exactly? Do you propose I use it instead of the factory?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Am trying to understand your requirement, correct me if its wrong:
So you have this- GUI and you need to set the Action (Event handlers?) for the GUI Components. But you dont want them to be within the GUI code?

If what I have understood is correct- You might have EventHandler classes for each Action- Say for action- "Save" you can have SaveListener which can implement required AWT Event Listeners.


Mohamed Sanaulla | My Blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

Usually, to share a common "action" one would extends AbstractAction and share the instance between all action sources.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
Mohamed, I'm not sure if I can use EventHandler to create instances of Action? I would like them to include icons as well.

Maneesh, this is essentially what I'm doing, but I'm just wondering what the best way is of sharing these instances.
I don't have a problem getting it to work. I'm just trying to organize it all in such a way that I separate my view and my controller effectively, and that my GUI class doesn't become too cluttered.

Let's say my GUI looks roughly like this:

Now, it's not too bad for two actions. But I'm going to add a lot more. How would you normally do it?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Ah sorry, I didnt know that Java had an EventHandler class as well. I meant- "Event Handler" (2 words) Something like- a Class implementing ActionListener.

You can always have a package for event handlers. As you said- You can have a factory for returning Singleton for each action- Say SaveAction, OpenAction and so on.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Stephan van Hulst wrote:I also just bumped into an interesting looking class named ActionMap. What is the purpose of this class exactly? Do you propose I use it instead of the factory?

I believe ActionMap is originally meant to be used with InputMap. When a user presses one or more keys, the matching key stroke is looked up in the InputMap. This will retrieve a value. This value is then used as the key of the ActionMap to find the matching Action. This is then executed.

Of course nothing prevents you from adding your own entries in the ActionMap, just be careful on what to use as keys. You don't want to overwrite any existing Actions or the behaviour of the component will change. Of course, if that's what you want that's not a problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
Rob, this shouldn't be a problem if I just create a new ActionMap, right?

Thanks all. Now I'm curious what everyone usually does. Do you personally use internal classes in your form, separate classes, something else?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Stephan van Hulst wrote: ...Now I'm curious what everyone usually does. Do you personally use internal classes in your form, separate classes, something else?


I mean separating would mean that lot of other parts of the code can reuse it. Also if we separate them then it can also create unnecessary noise in the code- If they are not used by other parts of the code. As long as all my GUI components are at one place- the action classes need not move out- they might as well reside along with the GUI code.

If at all I was using Netbeans or something else for building the GUI and then adding listeners- I wouldn't think much about how the code is being generated behind the scenes.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Stephan van Hulst wrote:Rob, this shouldn't be a problem if I just create a new ActionMap, right?

Nope. You can even give it a parent (e.g. an existing component's ActionMap) as fall-back.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
Thanks Mohamed. I have a good fresh perspective on the matter.

Rob, I ended up using an EnumMap of Actions. I'm surprised the designers didn't just use some standard Map themselves, but it might have to do with the fact that ActionMap was around before Generics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Separate Actions and GUI
 
Similar Threads
GUI 29 out of 40!!!
Java Reflection (or not)
Switching from .NET to JAVA help...
Abstract Factory Factory?
Basic invokeLater question