tags:

views:

262

answers:

4
+4  A: 

It depends on what you are trying to achieve. For example, I don't necessarily create a separate class for each JPanel. However if the panel is a custom(Overridden) panel, or a panel that contains a lot of other components, or contains a substantial amount of functionality then I put it in it's own class. I try to separate by functionality, if that makes sense. There is not particular reason I do it this way, other than after writing multiple gui's this seems to make things simpler and a lot cleaner. For instance in your example above, without knowing much about your program. I might create a component with a jPanel, the label, and the list for the components on the first row down, that way I could re-use the component w/ out rewriting the logic over and over.

broschb
Thanks broschb. That gives me a bit of direction. Just looking at examples such as this http://www.codeproject.com/KB/java/Graphics.aspx which have most things in the one class. I just thought there might be a cleaner way to do it ... but I guess not?
Aaron Moodie
+2  A: 

Javabuilders is my favorite way of doing simple GUIs. You declare your hierarchy of GUI elements + layout info in a YAML file which is much easier than explicitly creating windows manually and adding them to their parent elements.

I generally don't override standard GUI classes. It looks like you have a bunch of JLabels, JButtons, JLists, and JSpinners with some contained in JPanels. You can separate the task of creating all these windows properly in a hierarchy (which doesn't require any custom classes), from the task of managing their behavior, which is pretty easy by adding EventListeners to the particular components you are interested in managing.

The Javabuilders approach is nice because you can name all the components (these are internal names available to you as a programmer, not displayed), let it manage the window creation, and then access any components of interest programmatically by name, rather than by having to traverse the window hierarchy.

Jason S
Thanks for the link Jason. At this point I've got to had code it all as it's for uni, but might have a look at that after.
Aaron Moodie
A: 

If you are using Eclipse, I quite like the Jigloo GUI builder. It is free for non-commercial use and quite reasonably priced for a commercial license.

I tend to find I use something like Jigloo to visually set up all my components and add stubs for event handlers, then reopen the same file in the standard Java editor to put the code in behind it all. Works well for me.

Evan
+1  A: 

Make sure you understand the MVC pattern, specially in context of Swing.

Extending swing container classes (such as JPanel) typically is motivated by adding reusable application semantics to the component.

Beyond that, an extended (top level JPanel) is also a good candidate class for exposing 'control' functionality that manages the nested components. For example, in your gui, the top level container could be an extension class that has methods for managing the state of the nested panels based on user actions. (But note that you do not necessarily have to extend JPanel to manage the state of nested components).