[java] What is the difference between Swing and AWT?

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?

This question is related to java swing awt

The answer is


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.


  • 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.

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.

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.


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.


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.


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.

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.


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.

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.


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

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.


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


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.


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.


  • 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.

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


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.

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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to awt

How can I check that JButton is pressed? If the isEnable() is not work? How to Change Font Size in drawString Java Font.createFont(..) set color and size (java.awt.Font) Triangle Draw Method How to use KeyListener Drawing in Java using Canvas Java GUI frameworks. What to choose? Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot? Create GUI using Eclipse (Java) Getting a HeadlessException: No X11 DISPLAY variable was set Setting background color for a JFrame