Adam Bien's Weblog

Swing Is Especially Effective If:

  1. You know your target domain or business logic, so that you are able to design expressive and concise domain objects.
  2. You are thinking in "swing models", not the UI-components. If you understand your domain, you probably have an idea as well how to visualize the business concepts in object oriented way. The use of Swing models just becomes natural.
  3. You are starting with the domain objects covered with unit tests, then develop the Swing models - after that the work is almost complete. You "only" have to create the view and pass the model to it.
  4. You are NOT trying to work with the views directly and force them to behave in your way. This could become quite complex, cumbersome and very hard to maintain. E.g. if you are working with the JTable directly, instead of TableModel, even reordering the table columns can cause you some headaches. The are no issues with a custom model in this case.
  5. The use of TableCellRenderers and TableCellEditors in JTables helps you to visualize you domain objects directly - without any overhead.
  6. You are using visual designers like e.g. Matisse in pragmatic way and separate the generated, from your code.
  7. You have a clear concept how to bind (e.g. with reflection+annotations or JSR-295) the UI-components to domain objects and how and where to validate the input (e.g. in controllers, domain objects or converters). 
  8. Try to leverage existing JavaBean patterns like PropertyChangeListener, PropertyChangeSupport and the already built-in listeners in the UI-components. There is lot of stuff which can be reused.
  9. If you spend more, than half hour to solve a specific "Swing-problem", just stop hacking and use google to find a solution for your problem. There are amazingly good tutorials, articles and posts out there.
  10. If you loose your motivation. Just stop developing Swing, build a quick prototype with SWT/JFace for few hours (but use Tables and Trees). Your motivation should increase dramatically after this experiment :-)


NEW workshop: Building HTML 5 Applications Without Frameworks, Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Comments:

#10 lol!

Posted by mbien on November 26, 2008 at 11:28 AM CET #

Can you recommend a good book to learn Swing beyond just syntax? How to properly structure your code, MVC, the things you talked about, etc.

Posted by Ryan on November 26, 2008 at 03:25 PM CET #

Interesting post.

I have been thinking about learning some Swing. I have a couple of questions.

1. What do you mean by a "Swing model"?
2. Should I learn "raw Swing", or should I learn a Swing framework, like the NetBeans Platform or Spring RCP?

Posted by Eric MacAdie on November 27, 2008 at 02:34 AM CET #

Yes, I'd like know more about "best practices" for Swing. Thank's

Posted by alfred on November 27, 2008 at 05:09 PM CET #

There is a great book about swing - Swing tutorial! http://java.sun.com/docs/books/tutorial/uiswing/ It is complete and shows you almost all practices. The main sense of this post - don't think Swing is stupid, and author is completely right. It would have been better not to let Swing users use Swing without MVC model.

Posted by Aekold Helbrass on December 02, 2008 at 12:03 PM CET #

Adam,

Your first 4 points are perhaps the most important aspects when building any interface, not just one based on Swing. In my previous job, I had developed a framework for modelling our domain data (based on concepts borrowed from GDMO and LDAP) and developed generic Swing models (for tables, trees etc.) that could be driven from the model definition. It then became trivial to populate GUI elements with the object instances that were created on the basis of the data schema.

The entire GUI was also based on a plugin model that allowed for a very straightforward addition of new functionality to the system (a network management system for fiber optic networks).

To my mind, if you don't start with a clear view of the data you want to manipulate, you are destined to be frustrated in your attempts to present it to your end users, regardless of the GUI technology you use.

I was quite happy with Swing and hope to get back to it very shortly with a new open source version of my previous work. After spending 6 months working on a system in C# written by someone with no concept of MVC (or OO for that matter), I'm looking forward to becoming productive again ;-)

Regards,

Peter

Posted by Peter Strong on December 08, 2008 at 11:21 PM CET #

Adam,

PART1:

Your first 4 points are perhaps the most important aspects when building any interface, not just one based on Swing. In my previous job, I had developed a framework for modelling our domain data (based on concepts borrowed from GDMO and LDAP) and developed generic Swing models (for tables, trees etc.) that could be driven from the model definition. It then became trivial to populate GUI elements with the object instances that were created on the basis of the data schema.

The entire GUI was also based on a plugin model that allowed for a very straightforward addition of new functionality to the system (a network management system for fiber optic networks).

Posted by Peter Strong on December 08, 2008 at 11:23 PM CET #

PART 2:

To my mind, if you don't start with a clear view of the data you want to manipulate, you are destined to be frustrated in your attempts to present it to your end users, regardless of the GUI technology you use.

I was quite happy with Swing and hope to get back to it very shortly with a new open source version of my previous work. After spending 6 months working on a system in C# written by someone with no concept of MVC (or OO for that matter), I'm looking forward to becoming productive again ;-)

Regards,

Peter

Posted by Peter Strong on December 08, 2008 at 11:24 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License