I\'ve already realized some applications with a small graphical user interface. Nothing complex, but I\'ve encountered several problems that components aren\'t displayed or
I would suggest you to use netbeans for GUI development in AWT/SWING.
Regards, Sameer
NetBeans might the best option for building GUI in WYSIWYG manner but many java developers write their GUI by hands, as it is not that difficult. Care about the thickness of your borders and gaps between your controls and you're ok :)
I'm not a big fan of GUI builders: They typically autogenerate bucket-loads of code that then locks in your whole development team to using one IDE. Also, this code is often unreadable (check the code generated when using Matisse under Netbeans).
My recommendations for GUI design / debugging would be:
main
method to each panel (or "top-level" component) implementation, allowing other developers to easily determine what a component looks like.Action
s over ActionListener
s and register these actions with each JComponent
's ActionMap
. This allows them to be "extracted" and added to other parts of the UI (e.g. JToolBar
) whilst still having their state controlled by the "owning" JComponent
(i.e. loose coupling).assert SwingUtilities.isEventDispatchThread()
.TaskManager
class that is registered with my UI's status bar. Any background processing (performed within SwingWorker
s) is passed a handle to a Task
created by the TaskManager
. Interracting with the Task (by calling setDescription(String)
, setThrowable(Throwable)
, cancel()
) causes the status bar to be updated. It also causes the glass pane to be displayed for "global" tasks ... but this is all decoupled / hidden from the individual SwingWorkers.Observer
/ Observable
classes, but instead favour ChangeListener
, PropertyChangeListener
or your own custom listener implementation for propagating events. Observer
passes an Object
as it's event, forcing client code to check the type using instanceof and to perform downcasts, making code unreadable and making relationships between classes less clear.JTable
over JList
, even in situations where your table only has one column. JList
has some nasty features in its API including the fact that you need to provide a prototype value for it to calculate its size correctly.DefaultTableModel
as it typically results in you storing your "model" data in two places: In your actual business objects and also within the 2D array that DefaultTableModel
sits on. Instead, simply subclass AbstractTableModel
- It's very easy to do this and means your implementation can simply delegate through to the data structure (e.g. List
) storing your data.Apart from tools discussion, just some ideas and thoughts
I'm one of those archaic dudes who do GUI layout by hand. I'm also not afraid of the infamous GridBagLayout
!
I keep things simple for myself by emulating the coding standard used by Visual Age, years ago: I use a lot of JPanels to organize parts of the GUI, and each one gets its own makeXXX()
method to create it, lay it out and return it to a a parent panel or the constructor. That way, each makeXXX
only has to concentrate on a small part of the whole works.
Some components need to be accessible by various instance methods; I declare those as instance fields. The other stuff, that's just decoration or layout, need not be exposed outside the makeXXX
methods.
That's mostly it. Works for me.
Do it by hand. GUI builders aren't good unless you have the 'partial class' concept in C#, and even then they often cause more problems than they solve. Use the GUI builder tools to make a prototype - sure, but not for production code.
Also, another little trick I've used over the years to good effect when trying to debug layout or "which panel am I really seeing here" problems is to give each 'container' panel a really garish background color (yellow, blue, etc). Something obvious enough that you'll see it even if it's only one pixel wide.
And my favorite layout for simple dialogs is BoxLayout. It's not great, you have to write a lot of boilerplate, but at least it generally works the way you would expect it to in your head. Don't overthink layouts until you have to.