I'm struggling with the appearance of the code in the class for my GUI client that extends JFrame. In general, the methods of the class handle building text fields, buttons, labels, etc. and get the values to display from a controller class. However, at least half of the file is composed of about 12 inner classes that implement some sort of listener. I've written listeners to set default buttons based on focus, activate/deactivate buttons based on what is selected, and all kinds of other little things that make the GUI experience a little smoother. The inner listener classes need access to the member variables of the enclosing class to be able to manipulate them as described, so I can't think of any other way to make this work and to not make my code look (in my opinion) very ugly. I've never had to rely on such overuse of inner classes in any other application I've ever written.
Is this just what you have to do with swing, or is there a cleaner way? Maybe making the target member variables static protected, extending my ClientWindow class and implementing the listener interface... then I could create separate class files for each listener, but that seems like a sloppy work-around in itself.
Joined: Jul 21, 2008
I would make heavy use of Actions. Have an action for all actions a user will make, ie. search, delete, create etc. Inside the action you should have the call to the delegate and backend, and also any error pop up dialogs. If the action leads to another pop up dialog. Contain the dialog as an inner class of the action class.