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
Joined: Sep 20, 2010
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?
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, 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?
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.
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.