Difference Between AWT and Swing in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

What is AWT?

AWT also known as Abstract window toolkit. It is a platform dependent API used for developing GUI (graphical user interface) or applications that are window based. It was developed by Sun Microsystems In 1995 and is heavy weighted. It is generated by system’s host operating system and contains large number of methods and class which are used for creating and managing UI of an application. The main difference between AWT and Swing is that AWT is purely used for GUI whereas Swing is used for both, GUI as well as for making web application.

What is Swing?

In Java, a swing is a light-weighted, GUI (graphical user interface) that is used for creating different applications. It has platform-independent components and enables users to create buttons as well as scroll bars. It also includes a package for creating applications for desktops. The components of swing are written in Java and are a part of the Java foundation class.

Key Differences Between AWT and Swing

Package:

  • AWT: Part of java.awt package.
  • Swing: Part of javax.swing package.

Component Design:

  • AWT: Uses native system components, hence platform-dependent look.
  • Swing: Uses lightweight components, offering a consistent look across platforms.

Performance:

  • AWT: Generally slower since it’s tied to native GUI components.
  • Swing: Faster, more responsive due to lightweight design.

Customizability:

  • AWT: Limited customization for components’ appearance.
  • Swing: Highly customizable with features like ‘Look and Feel’.

Difference Between AWT and Swing

AWTSwing
In Java, awt is an API that is used for developing GUI (Graphical user interface) applications.Swing on another hand, in Java, is used for creating various applications including the web.
Java AWT components are heavily weighted.Java swing components are light-weighted.
Java AWT has fewer functionalities as compared to that of swing.Swing has greater functionalities as compared to awt in Java.
The code execution time in Java AWT is more.The execution time of code in Java swing is less compared to that of awt.
In Java AWT, the components are platform-dependent.The swing components are platform-independent.
MVC (Model-View-Controller) is not supported by Java awt.MVC (Model-View-Controller) is supported by Java swing.
In Java, awt provides less powerful components.Swing provide more powerful components.
The awt components are provided by the package java.awtThe swing components are provided by javax.swing package
There is abundant features in awt that are developed by developers and act as a thin layer between development and operating system.Swing has a higher level of in-built components for developers which facilitates them to write less code.
The awt in Java has slower performance as compared to swing.Swing is faster than that of awt.
AWT Components are dependent on the operating system.Swing components are completely scripted in Java. Its components are not dependent on operating system.
Java AWT stands for Abstract Window Toolkit.Java Swing is mainly referred to as Java Foundation Classes (JFC).
Java AWT includes 21 peers. Each control has its own peer, and there is an additional peer specifically for the dialogue. The operating system supplies these peers as widgets.Java Swing uses a single peer from the OS as a drawing surface to render its widgets like labels, buttons, and entry fields, all of which are created by the Java Swing Package.

Java Swing Hierarchy

In Java, the class hierarchy for all swing components is shown below in the following image:

SWING HIERARCHY

The object in Java swing also known as components is divided into two types:

  • Container- interface between component and code.
  • JComponent- abstract class that swing component extend. The container is the outer part of GUI, it is further divided into two parts:
  • Window- top-level layer
  • Panel- a subclass of window container The window has two objects, Frame and Dialog. The frame is used for outer covering while dialog also known as dialog box is used widely for user input and for output. While panel on the other hand has an applet that is used for embedding another program inside an application.

Java Swing Example

In this example, we will be creating a user form using the swing component of Java which is provided by javax.swing package.

Output: JAVA SWING OUTPUT

Explanation: In this, the swing package is first imported and its app is initialized. After this JFrame is initialized which is used for making application's window. The inner component of window like label, text area and buttons objects are initialized and used respectively. In the main method, the swing class object is initialized and called. The code then gets executed and the output is shown.

Java Awt Example

Consider a following example, In this we are building same application form as that of swing. The only difference is that we will be using AWT for building the app.

Output: OUTPUT AWT

Explanation: In the code given, the package is imported and class is extended by the main class. The awt function is called from the package and its components are initilized before using it. Components used are, label, text area and buttons. The method, setBounds are used for setting up the co-ordinates.The components are then added onto the window and the awt app is called inside the main function.

Conclusion

  • Swing is a light-weighted, GUI (graphical user interface) that is used for creating different applications.
  • AWT also known as Abstract window toolkit. It is a platform dependent API used for developing GUI (graphical user interface) or applications that are window based.
  • Java AWT components are heavily weighted whereas swing components are light-weighted.
  • Java AWT has fewer functionalities as compared to that of swing.
  • The execution time of code in Java swing is less compared to that of awt.
  • MVC (Model-View-Controller) is not supported by Java awt but is supported by swing.
  • The swing components are platform-independent whereas awt is platform dependent.