Re: Refactoring a Swing GUI

From: Harry Erwin (herwin_at_theworld.com)
Date: 11/26/03


Date: Wed, 26 Nov 2003 09:37:08 +0000

Ilja Preuß <preuss@disy.net> wrote:

> Harry Erwin wrote:
> > Here's a dumb question--how do you realistically go about refactoring
> > a javax.swing GUI? I've got a big one, and it smells to high heaven.
>
> Like everything else - in small, tiny steps.
>
> A refactoring I often do is finding groups of components which are often
> used (such as Label+TextField) and build a bigger, reusable component from
> it.

Similarly, but in my case the groupings tend to be just chunks of code
that don't lend themselves to reuse. I have a lot of JLabel + JComboBox
elements, which don't really lend themselves to reuse, and the Box stuff
is also heavily used.

>
> Do you have any more specific questions? What smells do you, well, smell?
>
> Take care, Ilja

Smells smelt:

1. The user interface looks like an airplane cockpit. I'd _like_ to make
elements of the UI appear and disappear on command. For example, I have
a list of elements displayed. I'd like it to show only the currently
defined elements, with an additional blank element that I can fill in.
I'd also like to be able to clear existing elements and have them just
go away. Unfortunately, the elements contain heterogeneous data, so the
standard tools are hard to use for this.

2. The coupling between M, V, and C is far too strong.

3. The code to put up any individual element of the UI is inelegant,
especially with the need to define event handlers for every detail. I
use inner classes a lot, but it still ends up with pages of code.

A friend tells me that OO was originally invented to ease the
development of UIs, but I come from the simulation community and have a
different perspective. As far as I can tell, UIs and OO are uneasy
siblings.

-- 
Harry Erwin <http://www.theworld.com/~herwin/>