Skip to main content

Questions about "Best Practices" in using Swing as UI layer

3 replies [Last post]
Joined: 2005-01-20

Hello everyone. I'm trying to create an architecture that is targeted to ease the efforts on developing Swing applications. I have a 3 year background on Web-Based applications, and about 1 year and half using JavaServer Faces.
The first thing I've noticed was that is not so trivial promoting a certain level of reuse in Swing. I'm trying to do something that I do in JSF - a BaseActionMessageHandler, that has methods to throw i18n messages to a customizable panel or to a JOptionPane, a BaseDataModelAction, that provides population utilities to DataModels, and a BaseFrameAction, that is injected with an entity, a service interface and has methods to execute operations on the service layer.
I want to use inheritance to provide template-like functionality to UIs, something like have a JFrame with all panels aligned correctly, and just adding (or injecting via IoC) the needed components in the frames.
I'm thinking on designing an utility base class to ease the notification of components about business layer events too.

Is this the correct approach? If not, can you experts show me the right path?
Thanks in advance

Rafael Mauricio Nami

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-09-06

> ... something like have a JFrame
> with all panels aligned correctly, and just adding
> (or injecting via IoC) the needed components in the
> frames.

I have done something similar in the past to build a POS system for a customer. My approach was to build a state machine. Each state was associated with:
. a layout container that defined the areas to show in the JFrame (think of a FRAMESET in html)
. a set of JPanels one to attach to each area (FRAME),
. and actions (including keystrokes).

When the machine moved into a certain state it ensured that:
. the correct frame set was displayed
. the correct panels were displayed in each area,
. the correct set of key-strokes were mapped to Actions in the JFrame.

My action classes worked a little like Struts Action and returned the name of the next state.

I implemented javax.swing.Action to invoke a front controller to do all this and nominate one of my action classes.

You can read me wobbling around the subject at I would loved to have had enough time to re-implement these ideas in an open project, but it has not been possible to date.


Joined: 2005-01-20

Thanks David.
I've read the other threads, and I'll DEFINATELY investigate your project (superficial).


Joined: 2005-08-11

Hi Rafael

There can be no doubt you raise some interesting points here. It’s arguable that your underlying concerns intersect with other current threads in this forum such as
‘Putting it all together’ and
‘Immutability vs. Beans’ There are also of course the recently-approved JSRs, 295 on databinding and 296 which promises a Swing application framework.

Perhaps what’s happening is that now Swing has finally matured into a serious widget toolkit, people are beginning to consider how to construct applications using it.

You will see that I have contributed to both threads with advertisements for Superficial , but this is not pure opportunism – I would hope that the ideas behind Superficial might contribute to a more general debate about how GUI applications might best be constructed.

So far as I can tell (and I’ve been looking for four years or so), there simply is no accepted set of Best Practices for Swing UIs, or indeed for GUIs in general. On the one hand there seems general agreement that the Model-View-Controller paradigm is helpful, but MVC is more a general principle than something that can be implemented as a practical API. Martin Fowler has presented some interesting potential patterns such as PresentationModel , but these also are very general.

On the other hand, Swing in particular provides a powerful, flexible API for constructing a widget GUI and capturing its input events. There is very useful support in Tree- and TableModel etc for connecting widgets to content. But when it comes to support for higher-level application architecture, Swing offers little more than the rather limited Action.

What’s missing is a well-understood set of concepts that help manage a GUI on something like MVC principles, and can be implemented in an API suitable for practical coding. Here’s my take on the spec, which I think reflects the concerns you mention:

* facilitate (as you imply) code reuse at the widget level, to minimise wasteful and error-prone boilerplate
* handle in some standard way the relaying of widget input to application content, and synchronising widgets with that content
* make it possible to have templates for common application constructs such as multiple viewers of the same content, or multiple content in the same application

I believe Superficial meets this spec fairly well, but there may well be other ways of doing it. The main thing is to establish a consensus about the underlying issues.

Regards, David Wright