jump to navigation

Enterprise Swing Best Practices August 6, 2009

Posted by Phill in Presentation Layer.
Tags: , ,

I know this blog is about J2EE, i.e. enterprise Java on the server side. But the past few months I’ve been working on an “enterprise” Swing-based desktop application. I say “enterprise” because it’s virtually the same as a server-side application – the only difference is that it’s using Swing for the presentation layer.

Anyway, I wanted to post up some of the things I’ve discovered: Swing is very easy, but it seems there aren’t many “best practice” guides for building an application using it. So, here is my list of “best practices”… hope you find it interesting!

  • The Presentation Model pattern is well worth using. It creates a much more loosely coupled dependency between the GUI code and the presentation logic.
  • On a similar note, using a binding framework (such as JGoodies Binding).
  • Use Actions (i.e. subclass javax.swing.AbstractAction) for use with menus and buttons etc.
  • For laying out forms, JGoodies Forms is awesome.
  • There are benefits to using component factories, i.e. a factory method to create your different UI components such as buttons.

I think that’s about it for the moment. The only other thing is, the Spring Rich Client Project (Spring RCP) looks really interesting, and I think if I was starting a new project I’d probably use it.

What are your favourite Swing tips?



1. Yves Zoundi - August 13, 2009

Nice post! I would say :
* Use a binding framework “as necessary” (JGoodies Binding is my favorites) or propertyChangeListeners or Obervable(s) objects.

* For laying out forms or panels, use whatever you’re comfortable with but if you find yourself building too many utility classes, consider using a different layout manager or implementing one.

* Actions are more flexible than ActionListeners, use it whenever you can

* Avoid too many singletons or constructors with too many parameters, IOC containers exist and it’s applicable in non J2EE applications. For the constructor problem, the builder pattern might be a good choice most of the time.

* Don’t follow the patterns just because they are popular or useful, use patterns wisely to solve your problems. Not everything has to follow a well known pattern such as MVC.

* If you start threads think about the event dispatching thread, consider using Spin, Foxtrot or SwingWorker or SwingUtilities.

* Don’t put all the logic, model, presentation in one file. It’s easy to use a GUI designer and double-click to add listeners, but think about who’s going to maintain the code and how the code will grow.

For UI factories, I would say “it depends” on what you’re doing. When there are not too many component extensions or implementations written, it’s overkill in my opinion.

2. Phill - August 13, 2009

Hi Yves, good thoughts! I forgot to mention SwingWorker – I discovered that class recently and have no idea how I lived without it 🙂

I guess most of these “best practices” are there to suit, i.e. use them if they are applicable in your application. Whether it’s applicable or not comes with experience.

I think component factories are useful in the app I’m working on at the moment – but obviously they’re not to be recommended in every case 🙂

3. Dalibor - November 18, 2009

A couple of swing related projects I can recommend are:
Swing Application Framework – https://appframework.dev.java.net/
MiG Layout – http://www.miglayout.com/

Sorry comments are closed for this entry

%d bloggers like this: