Best way to customize Java GUI

I am currently working on my first Java GUI programming project as a school assignment. I'm pretty familiar with customizing classes and methods in Java, although with an introduction to GUI programming I'm not sure how best to do this.

I have installed the GUI in my own package. Should I create a new class for each one JPanel

? I have attached a screenshot of what my GUI looks like.

alt text http://www.aaronmoodie.com/gui.png

Any advice on this would be really great.

+2


source to share


4 answers


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 many other components or contains significant functionality, then I put it in my class. I'm trying to categorize by functionality if that makes sense. There is no particular reason I do it this way, except after writing a few gui it seems to make things easier and much cleaner. For example, in the above example, not knowing about your program. I could create a component with jPanel, a label and a list for the components on the first line down, this way I could reuse the w / out component by rewriting the logic over and over.



+4


source


Javabuilders are my favorite way of making simple GUIs. You declare your hierarchy of GUI elements + layout information in a YAML file, which is much easier than creating windows directly by hand and adding them to their parent elements.

I am not overriding the standard GUI classes at all. It looks like you have a bunch of JLabels, JButtons, JLists and JSpinners, some of which are contained in JPanels. You can decouple the task of creating all of these windows properly in a hierarchy (which does not require any custom classes) from the task of managing their behavior, which is quite simple by adding EventListeners to whatever components you are interested in managing.



The Javabuilders approach is good because you can name all the components (these are internal names available to you as a programmer, not displayed), let it control the window creation, and then refer to any components of interest programmatically by name, rather than by moving through the hierarchy of windows.

+2


source


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

Extending swing container classes (like JPanel) is usually motivated by adding reusable semantics to the component.

In addition, the extended (top-level JPanel) is also a good candidate class for exposing the "control" functionality that manages nested components. For example, in your gui, the top-level container might be an extension class that has methods for managing the state of nested panels based on user actions. (But note that you don't need to extend the JPanel to manage the state of nested components.)

+1


source


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

I tend to think that I use something like Jigloo to visually set up all my components and add stubs for event handlers, then reopen the same file in a standard Java editor to put the code at its core. Works well for me.

0


source







All Articles