Sunday, September 21, 2008

Differences between Swing and AWT. When to use what?


Differences between Swing and AWT. When to use what?


"Swing" was actually the code name used by Sun during the development and it was planned that on release the extension will be known as JFC (Java Foundation Classes), but Swing managed to beat JFC in popularity even after the release of the package.


Swing package can be used with Java 1.1 as well. For that we need to include three JARs - swing.jar, swingall.jar, windows.jar in the CLASSPATH and the packages are named 'com.sun.java.swing.*', 'com.sun.java.swing.event.*', etc. In Java 1.2 the package names changed to 'javax.swing.*', 'javax.swing.event.*', etc. as the concept of extensions were introduced in Java 1.2 (Java 2 Platform).


Main Differences between Swing and AWT


Swing comonents are light-weight as compared to AWT components for the simple reason that they don't make use of the native UI components of the underlying platform.


Swing components haven been written completely in Java language. Not the same with AWT as it uses native libraries.


Swing components use relatively less number of classes as compared to AWT components not only because the it doesn't depend on native code, but also because it has a better design due to its evolution from AWT. Most of the design flaws got fixed which ultimately led to lesser redundancies.


The look and feel of the Swing components is defined only by the Swing extension classes and it's pluggable to have the look and feel of the underlying platform as well whereas the look and feel of the AWT comonents is mostly goverened by the underlying platform. By default, Swing components open up in their own look and feel.


Swing extension is completely based on the MVC (Model View Controller) architecture and hence the inherent benefits of the MVC design pattern add more value to the usage of Swing.


In addition there are few other trivial differences as well. For example: Swing extension has a richer set of components as compared to AWT, Swing's JPanel automatically gets double buffered and gets repainted faster, etc.


When to use what?


Due to all these advantages, Swing has almost entirely relaced AWT and you got to have a really strong reason if you are still using AWT in your application which is very hard to find at least for platform-independent applications.


Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


No comments: