SWT on Swing

2005-07-03, Stefan Matthias Aust

Although I'm quite happy with SWT on Windows, I started an experiment and tried to implement SWT based on Swing — just for fun and to learn how difficult that would be. I took SWT code snippet 1 (because those snippets seem to make up nice test cases) and wrote just enough code to make it compile and run. Then I continued with the next snippet...

What's This Project About?

I'm not sure whether it's a project at all. I've written some code to make quite a few SWT code snippets mostly working. I managed to get one of the official SWT examples (the address book application) to run but most other examples are too complex for my current code base.

The code uses Java 5 syntax and I currently use JRE 1.6.0 (b42) to develop, not making any attempt so far to restrict myself to an earlier JRE. All SWT code is from Eclipse 3.1. Everything will be published under the Eclipse Public License.

What's Working

The following list of snippets compile fine and seem to work:

Snippet 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 17, 18, 20, 24, 26, 27, 28, 29, 30, 32, 34, 35, 36, 40, 45, 50, 54, 56, 57, 59, 61, 63, 65, 69, 71, 72, 73, 76, 80, 94, 97, 99, 100, 102, 104, 105, 107, 108, 109, 112, 130, 150, 169, 172, 175, 176, 177, 182, and 183. Click here for details.

Futhermore, I can run the five hello world applications and the address book application from the official SWT examples. The file viewer application starts, but I've threading issues so it doesn't work. The layout example application starts, but most function is just stubbed out and it has layout problems.

And What's Not

I had and still have problems to correctly run Swing code in the AWT dispatcher thread and SWT related code in the SWT UI thread. At first I didn't bother at all and then later tried to fix that but it's still only working 80% of the time. I've two implementation and some snippets work only with one, some only with the other implementation.

No class is API complete. I've implemented only what was needed to compile and run the code. A lot of code is only stub code and there're a lot of TODOs left. Dynamically changing the layout will fail.

Lessons Learned

I haven't looked into printing and accessibility, but otherwise doing a Swing port of SWT widgets isn't that difficult. It's quite difficult to get the threading issues solved, though! SWT has an explicit dispatch loop inside each application and another explicit loop is used to make dialogs modal or to stop processing and wait for some event. Swing works completely different, using a background thread to deal with all UI events. You can't stop the AWT dispatch thread and (as far as I currently know) you can't easily replace it. So you need to somhow combine and synchronize two different threads.

SWT is a moving target and changes to the official code base must be monitored carefully and added to each platform port — including a possible Swing port. Only a very small part of SWT (mainly layout managers and event objects) is platform independent.