Java, Large GUI classes, with many ActionListeners; best way to organize the listeners?
actionlistener, java, swing
Solution
You could try the following approach:
Use real classes instead of anonymous classes. Every ListenerClass implements only one Use-Case / Functionality. The Classname should describe the UseCase. Then you can organize the classes in one or more packages to cluster them by categories that fit to the Use-Cases that the Listener in the package implements.
That means, you create an hierarchy of abstraction, that organizes the functionality like a tree-structure.
If some day later somebody has to maintain the Listeners he/she can find the Listener by first looking for a package that fits to the UseCase and then for the UseCase itself. Since you will have less packages then Classes it will be easier and faster to find the Listener.
Another way to think about that: If you have so much Events on one Tab, that you get problems organizing them in the code, how do you organize them visually on the Tab? Can you handle that in an ergonomic way for the user? Maybe the solution could be in splitting the functionality on more then one Tab? But since I don't know your UI I cannot say too much about that
Problem
I've been developing java programs for 1½ year. I'm currently working on a summer project, that involves quite a big Graphical User Interface. My GUI consists of several tabbed panes. Each pane has its own class. Each pane has SEVERAL jButtons. Now, I've come to a point, where there's so many anonymous inner classes (for ActionListeners) in my tabbed-pane classes, that I am certain there must be a better way; if not for efficiency, then for maintainability - it's becoming quite a mess. My question is this: Is there a better to organize listeners, when you have a lot of them in each class? I've thought about clustering the listeners in relevant classes - like the following sample code: ``` public class SomeListeners implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ String command = e.getActionCommand(); switch(command){ case "This button": doThis(); break; case "That button": doThat(); break; } } } ``` Or might there be an even better way? Thanks in advance :)