tags:

views:

1559

answers:

4

Can some one please explain me what is the difference between swing and awt?

Are there any cases where awt is more useful/ advised to use then swing or vice-versa?

Thanks in advance.

+3  A: 

Swing vs AWT. Basically AWT came first and is a set of heavyweight UI components (meaning they are wrappers for operating system objects) whereas Swing built on top of AWT with a richer set of lightweight components.

Any serious Java UI work is done in Swing not AWT, which was primarily used for applets.

cletus
are there any cases where awt is more useful/ advised to use then swing?
Samiksha
Samiksha, make that another question instead of trying to have a discussion thread in these comments. :)
skiphoppy
It used to be relevant... 10 years ago.
bart
+15  A: 

Awt is a Java interface to native system GUI code present in your OS. It will not work the same on every system, although it tries.

Swing is a more-or-less pure-Java GUI. Using awt it creates an operating system window, and then it paints pictures of buttons, labels, text, checkboxes, etc., into that window and responds to all of your mouse-clicks, key entries, etc., deciding for itself what to do instead of letting the operating system handle it. Thus Swing is 100% portable and the same across platforms. (Although it is skinnable and has a "pluggable look and feel" that can make it look more or less like the native windows and widgets would look.)

These are vastly different approaches to GUI toolkits and have a lot of consequences. A full answer to your question would try to explore all of those. :) Here are a couple:

Awt is a cross-platform interface and so even though it is using the underlying OS or native GUI toolkit for its functionality, it doesn't provide access to everything that those toolkits can do. Advanced or newer widgets that might exist on one platform might not be supported. Features of widgets that aren't the same on every platform might not be supported, or worse, they might work different on each platform. People used to invest lots of effort, including trying to make calls into native code from Java, to get their Awt applications to work consistently across platforms.

Because Awt uses native GUI widgets, your OS knows about them and handles putting them in front of each other, etc. Whereas with Swing your widgets are just meaningless pixels within a window; Swing handles deciding how your widgets lay out and stack. Mixing the two is highly unsupported and can lead to ridiculous results, such as native buttons that obscure everything else in the dialog box in which they reside because everything else was created with Swing.

Because Swing tries to do everything possible in Java other than the very raw graphics routines provided by a native GUI window, it used to incur quite a performance penalty over Awt. This made Swing unfortunately slow to catch on. However, this has shrunk dramatically over the last several years, due to more optimized JVMs, faster machines, and (I presume) optimization of the Swing internals. Today a Swing application can run fast enough to be serviceable or even zippy, and almost indistinguishable from an application using native widgets. Some will say it took far too long to get to this point, but most will say that it is well worth it.

Finally, you might also want to check out SWT (the GUI toolkit used for Eclipse, and an alternative to both Awt and Swing), which somewhat of a return to the Awt idea of accessing native Widgets through Java.

skiphoppy
:) .. please check my edited question
Samiksha
Um... having done some pretty extensive Swing across multiple platforms, I can tell you that it very much is not the same across platforms. Similar? Sure. Same? No way.
cletus
thanks a lot skiphoppy...
Samiksha
The heavyweight/leightweight problems will disappear with Java 6 update 12 (see http://java.dzone.com/news/a-farewell-heavyweightlightwei).
Dan Dyer
Wow. I can't believe they can fix it, and I still can't believe mixing lightweight and heavyweight components would ever be desirable. But it's incredible that they can fix it.
skiphoppy
Just forget about both. Have a look at WPF. :)
Alper Ozcetin
+3  A: 

The base difference that which already everyone mentioned is that One is heavy weight and other is light weight. Let me explain, bacially what the term heavy weight means is that when you are using the awt components the native code used for getting the view component is generated by the Operating System, thats why it the look and feel changes from OS to OS. Where as in swing components its the responsibility of JVM to generate the view for the components. Another statement which i saw is that swing is MVC based and awt is not.

Access Denied
+3  A: 

As far as when AWT may be more useful than Swing -

  • you may be targeting an older JVM or platform that doesn't support Swing. This used to really come into play if you were building Applets - you wanted to target the lowest common denominator so people wouldn't have to install a newer Java plugin. I'm not sure what the current most widely installed version of the Java plugin is - this may be different today.
  • some people prefer the native look of AWT over Swing's 'not quite there' platform skins. (There are better 3rd party native looking skins than Swing's implementations BTW) Lots of people preferred using AWT's FileDialog over Swing's FileChooser because it gave the platform file dialog most people were used to rather than the 'weird' custom Swing one.
Nate