What is the difference between Swing and AWT?
Asked Answered
C

9

249

Can someone please explain me what's the difference between Swing and AWT?

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

Clavier answered 3/1, 2009 at 9:43 Comment(1)
Related: https://mcmap.net/q/118819/-is-java-swing-still-in-use-closed/…Kristianson
C
260

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. It uses AWT to create an operating system window and then 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 is the same across platforms (although it is skinnable and has a "pluggable look and feel" that can make it look more or less like how 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, so even though it uses 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 AWT widgets that might exist on one platform might not be supported on another. Features of widgets that aren't the same on every platform might not be supported, or worse, they might work differently on each platform. People used to invest lots of effort to get their AWT applications to work consistently across platforms - for instance, they may try to make calls into native code from Java.

Because AWT uses native GUI widgets, your OS knows about them and handles putting them in front of each other, etc., whereas Swing widgets are meaningless pixels within a window from your OS's point of view. Swing itself handles your widgets' layout and stacking. Mixing AWT and Swing 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 compared to 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 is somewhat of a return to the AWT idea of accessing native Widgets through Java.

Comp answered 3/1, 2009 at 9:51 Comment(5)
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.Hanghangar
The heavyweight/leightweight problems will disappear with Java 6 update 12 (see java.dzone.com/news/a-farewell-heavyweightlightwei).Cesspool
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.Comp
Just forget about both. Have a look at WPF. :)Gelatinoid
Here's an official Java article confirming that mixing is fixed: oracle.com/technetwork/articles/java/…Rustle
M
37

The base difference that which already everyone mentioned is that one is heavy weight and other is light weight. Let me explain, basically what the term heavy weight means is that when you're 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.

Mallory answered 3/1, 2009 at 10:48 Comment(1)
actually Swing uses a Model-Delegate approach, which is derived from the MVC approach, where in the View and Controller are combined for a Delegate structureDufrene
H
18

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.

Hanghangar answered 3/1, 2009 at 9:48 Comment(4)
are there any cases where awt is more useful/ advised to use then swing?Clavier
It used to be relevant... 10 years ago.Helbonia
@Pacerier he was talking about AWT, not SWTTopgallant
Unbroken article link edn.embarcadero.com/article/26970Hiding
C
12

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.
Cassaundra answered 7/7, 2009 at 2:56 Comment(1)
But for the last one, we can also create a FileChooser that pretty much looks like windows file dialog (but with no autocomplete) https://mcmap.net/q/118820/-how-to-display-default-system-icon-for-files-in-jfilechooser/2534090Industrial
I
10

Several consequences result from this difference between AWT and Swing.

AWT is a thin layer of code on top of the OS, whereas Swing is much larger. Swing also has very much richer functionality. Using AWT, you have to implement a lot of things yourself, while Swing has them built in. For GUI-intensive work, AWT feels very primitive to work with compared to Swing. Because Swing implements GUI functionality itself rather than relying on the host OS, it can offer a richer environment on all platforms Java runs on. AWT is more limited in supplying the same functionality on all platforms because not all platforms implement the same-looking controls in the same ways.

Swing components are called "lightweight" because they do not require a native OS object to implement their functionality. JDialog and JFrame are heavyweight, because they do have a peer. So components like JButton, JTextArea, etc., are lightweight because they do not have an OS peer.

A peer is a widget provided by the operating system, such as a button object or an entry field object.

Ingunna answered 29/5, 2013 at 9:35 Comment(0)
W
8

Swing:

  1. Swing is part of the java foundation classes.
  2. Swing components are platform-independent.
  3. Swing components are lightweight components because swing sits on the top of awt.

AWT:

  1. AWT is called the abstract window tool.
  2. AWT components are platform-dependent.
  3. AWT components are heavyweight components.
Wordless answered 21/2, 2013 at 15:18 Comment(0)
C
5
  • swing component provide much flexible user interface because it follow model view controller(mvc).
  • awt is not mvc based.
  • swing works faster.
  • awt does not work faster.
  • swing components are light weight.
  • awt components are heavy weight.
  • swing occupies less memory space.
  • awt occupies more memory space.
  • swing component is platform independent.
  • awt is platform dependent.
  • swing require javax.swing package.
  • awt require javax.awt package.
Cystoscope answered 31/1, 2013 at 13:51 Comment(1)
I don't think swing works faster than AWT because AWT uses native code (the gui code) that was already there in the OS but swing builds every component from the scratch. So AWT might be faster. Could you say me what is your point of view in saying swing works faster? Thanks.Industrial
R
3

AWT 1 . AWT occupies more memory space 2 . AWT is platform dependent 3 . AWT require javax.awt package

swings 1 . Swing occupies less memory space 2 . Swing component is platform independent 3 . Swing requires javax.swing package

Reduplication answered 11/4, 2013 at 13:37 Comment(2)
Could you say me how does AWT occupies more memory space? Because it uses native code?Industrial
awt requires java.awt.*Gnu
I
0

Java 8

Swing

  • It is a part of Java Foundation Classes
  • Swing is built on AWT
  • Swing components are lightweight
  • Swing supports pluggable look and feel
  • Platform independent
  • Uses MVC : Model-View-Controller architecture
  • package : javax.swing
  • Unlike Swing’s other components, which are lightweight, the top-level containers are heavyweight.

AWT - Abstract Window Toolkit

  • Platform dependent
  • AWT components are heavyweight
  • package java.awt
Impedance answered 27/11, 2020 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.