I’d never really been one to subscribe to the popular perception that swing is so horrifically slow that it isn’t suitable for use in any serious application. Most of the problems I’ve seen with swing based programs have resulted from people not knowing how to make processing tasks run in a separate thread and therefore causing the interface to block until they complete.
For some reason I seem to have got into a habit of writing java applications which operate on enormous datasets and which stretch swing to its limits. For the most part it has coped very well, but this last week provided me with a display where the lag in swing was noticable.
Although the display didn’t update very often I did need to repaint to highlight one part of the screen and the lag from clicking to the display changing was around a second – which was too long. All of the time was spent in the swing redraw code (Sun’s part – not mine!).
Bizarrely when I moved the program onto my mac the interface was very snappy – no lag at all, and it was the same on my linux box, so there was something odd about the windows drawing code.
A while ago I remembered reading that Sun had added OpenGL 2D acceleration into swing. Since this seemed a good time to look into this I read some of the Sun documentation about this. To cut a long story short OpenGL is present from java 1.5 onwards, but is disabled by default. To enable it you simply need to add the following as a JRE argument:
On my application this had a dramatic effect. The redraws which previously took around a second were now virtually instant. I’ve since applied this to a couple of other large applications and all have shown a noticable increase in responsiveness.
It’s not every day you come across a magic bullet, but this came pretty close as far as I was concerned.