Swing programming not much fun either

In one of my first blog entries (Web programming == No Fun) I moaned about the relative painfulness of creating a web-based user interface compared to doing the same thing in a desktop UI framework such as Java Swing (which I was most used to at the time).

Now, a few years (but not that many blog entries) later I find myself in the opposite situation. I’ve spent most of the intervening years building web applications in Java, Javascript and PHP and I found myself needing to dust of my desktop-UI skills again for some work I was doing on my Hexapod robot project.

I’m now at the stage where the appeal of typing commands to the servo controller using a terminal emulation and a serial link has worn off somewhat. Yes, I can make the robot twitch a bit and the mechanics seem to be working but some more interesting behaviour would be a definite plus. It seemed like a good idea to write some software I could run on my PC to configure and control the robot so I could learn how to program interesting behaviours without having to learn a new embedded programming environment at the same time. It wasn’t long before I decided I needed a file format to store the robot configuration in - things like mapping meaningful names to the numeric IDs used by the servo controller and storing the min and max joint angles for each servo etc. After messing about for a while with a terminal emulator and the robot I decided that I needed some form of interactive interface which would let me easily set up a configuration and manipulate the robot to find the correct servo settings.

Without a great deal of thought I picked Java (could have been C++ or ruby but I kind just settled with Java out of habit) and Swing (AWT is a UI toolkit that should never, IMHO, have seen the the light of day and I’ve no experience with SWT).

Putting on my rose tinted glasses and remembering how much I used to enjoy Swing programming I fired up eclipse and had a bit of a shock - somehow Swing programming didn’t seem nearly as much fun as I’d remembered. Having gotten used to layout out my user interfaces with HTML and CSS using Swing layout managers, for example, seemed like an exercise in masochism.

So what has happened? Why the shock? Well, firstly (and most obviously) whatever we are most used to often feels easiest (duh!). When I wrote Web programming == No Fun I was still pretty new to Java web development, especially STRUTS and had recently developed a fairly complex Swing application. Now I’ve had a few years to forget a lot of my Swing knowledge and am a more experienced web developer.

I think there’s more to it then just familiarity though. Web development has changed quite a bit for me in the last few years. Although I started out with STRUTS more recently I’ve used Spring MVC (and a proprietary framework at Ecube Ltd, where I’m presently employed). I would argue that STRUTS, at least the version I was using back in 2005, is painful and involves the creation of far too many pointless ActionForm classes and XML configuration [2]. Personally, I find SpringMVC far better suited to the way I want to write code.

Libraries such as prototype, scriptaculous and YUI and so on have made it a lot easier to incorporate more complex user interface widgets in web applications. Also, the event based nature of AJAX programming it’s not that different from the event based programming you’d do for a Swing application. Sure, the actual events are different but it’s a not the same as spending your time building a UI in which you have to (re)build the complete page as a response to every significant user action.

I think Swing [3] is badly lacking the ability to separate structure from appearance that you gain with HTML, Javascript & CSS. Actually getting things to look good and appear where you want them to can be painful with both Swing and HTML/CSS but at least with HTML it’s easy to knock up a functional UI and tweak the appearance without having to rewrite too much of the code that actually generates the UI. With Swing how things look is too closely bound with the code that creates the widgets.

Swing does perhaps have the edge when it comes to complex widgets such as collapsible trees which are updated when the underlying developer-supplied model changes but that also brings additional complexity and I spent a good few hours wrestling with JTree and the associated TreeModel and events before I got things working the way I wanted.

I suppose I could have used a GUI builder when writing my little configuration app but I’m suspicious of such tools and find that, at best, they are good for producing a first cut of the UI after which you usually end up tweaking some fairly opaque and/or verbose code by hand. In the same way I code HTML & CSS by hand also - it may be more painful at times but at least I remain in control.

BTW if you want a look at the little app that inspired these ramblings have a look at Robot Configuration - it’s still somewhat krappy and incomplete but it’s progress of a sort anyway.