File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes GUI Design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "GUI Design" Watch "GUI Design" New topic

GUI Design

Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
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.

SCJP 5.0
Oliver Watkins

Joined: Jul 21, 2008
Posts: 13
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.

That way your code will look lots cleaner
I agree. Here's the link:
subject: GUI Design
It's not a secret anymore!