AWT, SWT, Swing: Java GUI Clean Up (1)
Overview
Java GUI toolkits have always been a controversial topic. The same type of debates also happened in other languages such as Smalltalk. In fact there exist such debates on every platform-independent language. It is especially prominent in Java because Java is the most dominating language today.
So what’s the controversy? Basically, it is a dispute between people who support emulated components(or widgets, or controls) and those who support native components. There are two camps among Java developers, one of which advocates for emulated components as in Swing, and the other native components as in SWT.
History
There are many stories about around the dispute on the Internet. You should probably have already heard of them. One of them can help you to understand the whole picture. Let’s start with this one, in which Amy Fowler from Swing team is one of the protagonists.
Back in 1990s, there were three companies, which developed their products using Smalltalk. They are IBM, Digital Talk and Parc-Place. IBM and Digital Talk adopted native components, while Parc-Place believed that emulation is the way to go. At the very beginning, all things went well until IBM overtook the others. The other two then merged to form a new company named Objectshare. Then a huge battle erupted when they tried to merge their products into one. The native and emulated crowd fought the battle nearly to death. When Amy Fowler from Parc-place, who insisted on emulated components, finally won the victory, IBM had got all of their accounts, because the two companies did nothing but quarrel for an entire year. The share price of the company, went down to under 1 dollar a share and were pulled from NASDAQ because of incorrect financial reportings.

At that time, AWT had just existed. Sun had built a basic set of portable control classes that mapped to native components on the different operating systems. However AWT was very buggy. It was beyond belief this was just poor code that could be fixed. Just then, Amy was hired by Sun, and she promised to solve all of the problems by doing a lightweight solution. Convinced, Sun management to make her the head of the GUI development. Amy then hired all her old Parc-Place friends and they set about creating Swing.
In IBM, Visual Age for Java were first written in Smalltalk, which used native components. Then they started to migrate them to a Java code. All of the IBM developers are the those Smalltalk guy, and they hated Swing for its emulated nature. They reluctantly built it using Swing. At that time, it is of no doubt that Swing was ugly, slow and buggy. Therefore they created a project to migrate the Smalltalk native components over to Java. This toolkit was later called SWT, which initially means Simple Widget Toolkit and later Standard Widget Toolkit. It was a success as they released a product called Visual Age Micro Edition. The folks later found that there were bugs in Swing reading windows events, which could cause memory leaks. So they took the decision that SWT and AWT/Swing should not co-exist and then put the toolkit into Eclipse which is a tools platform derived from the early Visual Age.

The above story should have given you an overview of the history of the three, especially SWT. Now you might think, the reason IBM did to create SWT was valid and Swing should follow the way SWT has been on. Actually this opinion is very superficial. When you dwell upon to the nature of Java, you will find that it is not that simple as you expect.
Prerequisite
What is the essential feature of Java, which has influenced the decision in the toolkit design? Or what is the prerequisite of a java GUI toolkit?
The answer comes from one of Sun's promises about Java, write once, run anywhere. It is one of the java's advantages over other languages. Before Java was created, software portability was a nightmare especially to those who wanted to support multiple platforms. It is especially true in modern days when the Internet is so popular. People from different parts of the world are working on different platforms. And it is very common for a software vendor to support multiple operating systems. Java’s write-once-run-anywhere promise was obvious a relief to developers. It can greatly improve software development productivity.
However to write a portable applications, you should use those standard libraries, which provide platform-independent APIs. These standard libraries include language support, common utilities, networking, I/O and GUI toolkit, etc. So when Sun started to design the GUI toolkit, the first thing it should consider is a well-designed platform-independent API. AWT and Swing were designed carefully in such a way to avoid platform incompatibility. SWT on the contrary was initially designed without this portability in mind. It was first designed for an IDE, Visual Age for Java. And at that time, it seemed that Windows was their first priority. Therefore SWT API is more akin those of Windows. In general SWT is not as portable as Swing. Although Steve Northover, the father of SWT, argued that SWT is platform independent, you can easily find many Windows API inheritances.
Differences
GUI application is one of the major types of softwares. So Java GUI library should be standardized and integrated into the JRE platform. However different operating systems have different GUI styles and component sets. There are some components which exist on all the platforms and have similar look and feels. These common components such as button, label, textfield, checkbox, etc are also called standard components. Different gui toolkits provide different set of components. Same component from different toolkits may have different look and feel. GUI toolkit usually follows different principle when choosing component types and features to implement. When examining a gui toolkit, we generally have two different levels of concerns. One is component types, the other is component features.
Terms
First let me illustrate two mathematical concepts to you: LCD and GCD. LCD means least common denominator. GCD means greatest common denominator. Look at the figure below. There are three different sets standing for different Oses. The intersecting part is LCD of the three, while the union is GCD of the three.

Component Types and Features
Now let’s examine the component types and features of the three Java GUI toolkits, AWT, SWT and Swing.
AWT
AWT component set complies LCD principle, which means AWT only has the common set of components which exist on all platforms. So you cannot find advanced components such as table or tree in AWT, because these components are not supported on some other platforms. As to feature set per component, AWT complies LCD too. It can only support those features available from all platforms. For example, AWT buttons is not able to be attached with an icon, because on Motif platform, button is not supposed to have an icon.
Since its poor component and feature support, AWT did not attract developers much. And it is deprecated by Sun. It is only there to ensure backward compatibility and support Swing.
SWT
One of SWT’s initial goal is to provide a more rich component set than AWT. It adopts GCD (greatest common denominator) principle to provide a union set of components that appear on every platform. The idea is that if a component type exists on the platform, then SWT will encapsulate it using java code and JNI call. If a component does not exist on the platform, it then emulates the component by extending and drawing a Composite. A SWT Composite is similar to AWT Canvas. By this way, SWT provides a richer component set than AWT. It is worth to point out that SWT JNI wrapper is different from AWT. Its emulation is also different from Swing.
As to component feature set, SWT is similar to AWT. They complies to LCD. In early SWT versions, SWT button did not support icon attachment for the same reason as AWT. But later, many of those missing features were made up using emulations. But still, there are features which cannot be implemented purely by emulation. SWT has its components completely controlled native operating system. It is hard to extend. Only features like some graphic decoration can customized by emulating. So strictly speaking, SWT component feature set can not be as rich as Swing due to its difficult to extend.
Swing
Swing is the most powerful and flexible of the three. With respect to component types, Swing complies to greatest common denominator. Because Swing controls all of the GUI system and it is very extensible and flexible, Swing can almost create any component you could imagine. The only limitation of Swing is its AWT containers. In Swing you still can not implement real transparent or irregular-shaped windows, because Swing depends on those AWT top containers including Applet, Window, Frame and Dialog etc. Except these niches, Swing has implemented almost all the standard components on every platform.
As to component feature set, Swing complies to greatest common denominator. It has most of the component features available on every platform. What's more, you can extend the existing Swing components and add more features to them.
(To be continued ...)
Up!
发表于 aa 在 2007年11月23日, 08:25 下午 CST #
你好,有个问题想问一下,有个朋友让我帮作一个程序,功能挺简单,有一个地方不知道怎么办。其实就是一个发货单的打印,一式三联的那种,就好像交手机费时的那样,开始单子上的表格是有的,就是要把在界面中填入的数据打印在相应的位置上,不知道应该怎么处理打印的部分,想教一下,谢谢!!
发表于 小新 在 2007年11月28日, 08:33 下午 CST #
my Blog: http://blog.sina.com.cn/software01
Thanks for you attention!
发表于 aa 在 2007年12月02日, 09:59 下午 CST #
Thanks for you attention!
发表于 肺癌 在 2007年12月05日, 09:45 下午 CST #
博主,你好!好久没看见你发表文章了。说真的,对你在swing方面的造诣在下佩服的无法用语言形容。可以说是炉火纯青了,估计国内在swing方面有你这样技术的人真没几个。我应该算是你的老fans了,你以前在sina上开博的时候就经常光顾了。说真的,新浪那破博客真不怎么滴,速度慢不说,而且还有广告,布局什么的都很烂,都是生活上的博客,高兴的是博主把转移了阵地,但是一直有疑问的是,博主难道想把sina上那么多的文章都丢掉吗?那么多的好文章丢弃了多可惜啊?为什么不转到这个blog上呢,博主在sina的文章我基本每一个都拜读过了,而且大部分也都收藏了,但是希望让更多的人看见你的文章,让更多的人开始了解swing,使用swing,喜欢上swing,所以建议你还是不要扔掉,真的太可惜了。
发表于 ivin 在 2007年12月14日, 09:40 上午 CST #
hi Ivin,
我要离开Sun了,到其他公司工作。没有时间写文章了。等稳定之后再来写文章。还有一个问题就是,这个博客可能要被收回了。所以我就没有转移文章。等以后有时间了,我会换一个地方写博客,所以还要等一段时间。
发表于 WilliamChen 在 2007年12月19日, 03:32 下午 CST #
hi, 博主
这些天每天都要到你的blog上看看,不过看到没有新的文章,所以就没有继续看。今天才发现了你的留言,原来博主这些天这么忙啊。
和ivin 一样,我也是博主的铁杆fans,从知道你的blog那天起,几乎每天都要到你的blog上看看。把你的好多文章都打印下来了看。
对博主的佩服和感激真是无以言表。
希望博主可以找到更好的工作,作的更开心。生活的更快乐。
也希望可以看到博主更多的文章。
发表于 218.94.70.142 在 2007年12月19日, 10:26 下午 CST #
楼主你好,听说您要离开Sun真不知道说什么好,但是无论到哪您都是精英。我在BEA的时间也不会太长,要去创业了,技术嘛,仍然是IM通讯终端、Swing、外加Flex。看来这辈子是和Java Web无缘了,至少不会像大多数人一窝峰搞什么Structs,spring,Habernate、Ajax这些技术。
今后有时间想系统地看看OSGI,还有ActionScript,虽然学Flex没多久,但是我发现编程的思维方式和Java桌面编程极其相似,语法和Java很相象,而且ActionScript可以支持Socket协议,这个功能真是太强大了,对于Flex vs Ajax,我觉得Ajax的优势在于它是基于文本的,而浏览器直接处理的也只是文本,所以它的体积会小一些,您看呢?
aa,你的Blog http://blog.sina.com.cn/software01我看了看,很不错啊,相信你也是个C++高手吧,佩服。我一直期待着在未来的JDK AWT中加入非巨型、本透明窗口的支持。
发表于 电玩 在 2007年12月21日, 10:38 下午 CST #
hi, 博主,你好!
这几天一直有点忙,今天才有空来这看看。没想到你要离开sun了,真和 电玩 一样的感觉,不知道该对你说点什么才好!希望你能够找到更好的地方去发挥你的才能。同样和 电玩 一样相信你无论走到那里都是精英。期待你到一个新的环境里稳定之后放出更多的好文章!我会一直关注你的!
小小的建议:建议你把blog安家在blogjava吧(http://www.blogjava.net),一个是因为比较稳定,二是因为blogjava的气氛比较活跃,这样文章传播的效果会比现在明显的多。要不每次blog的迁移就会损失一些好文章,岂不可惜。
祝福博主找到更加理想的工作。
to: 电玩 我也算是 你的老fans了,你的文章也都一一拜读过了,blog也经常去,貌似你最近比较忙啊,blog也没更新了。
发表于 ivin 在 2007年12月25日, 04:00 下午 CST #
另外博主如果blog换地方了能否麻烦告知一下?mail:ivin2006@gmail.com
发表于 ivin 在 2007年12月25日, 04:03 下午 CST #
发表于 quxiling 在 2008年04月01日, 10:36 上午 CST #
太好了,我正要英文的论文呢,毕业设计让翻译。
发表于 王者无敌 在 2008年05月31日, 09:13 下午 CST #
发表于 世迈软件工作室 在 2009年05月13日, 10:03 上午 CST #