AWT and Swing in Java
AWT and Swing are used to develop window-based applications in Java. Awt is an abstract window toolkit that provides various component classes like Label, Button, TextField, etc., to show window components on the screen. All these classes are part of the Java.awt package.
On the other hand, Swing is the part of JFC (Java Foundation Classes) built on the top of AWT and written entirely in Java. The javax.swing API provides all the component classes like JButton, JTextField, JCheckbox, JMenu, etc.
The components of Swing are platform-independent, i.e., swing doesn't depend on the operating system to show the components. Also, the Swing's components are lightweight. The main differences between AWT and Swing are given in the following table.
||The AWT Component classes are provided by the java.awt package.
||The Swing component classes are provided by the javax.swing package.
||The Components used in AWT are mainly dependent on the operating system.
||The Components used in Swing are not dependent on the operating system. It is completely scripted in Java.
||The AWT is heavyweight since it uses the resources of the operating system.
||The Swing is mostly lightweight since it doesn't need any Operating system object for processing. The Swing Components are built on the top of AWT.
||The Appearance of AWT Components is mainly not configurable. It generally depends on the operating system's look and feels.
||The Swing Components are configurable and mainly support pluggable look and feel.
|Number of Components
||The Java AWT provides a smaller number of components in comparison to Swing.
||Java Swing provides a greater number of components than AWT, such as list, scroll panes, tables, color choosers, etc.
||Java AWT stands for Abstract Window Toolkit.
||Java Swing is mainly referred to as Java Foundation Classes (JFC).
||Java AWT has 21 peers. There is one peer for each control and one peer for the dialogue. Peers are provided by the operating system in the form of widgets themselves.
||Java Swing has only one peer in the form of OS's window object, which provides the drawing surface used to draw the Swing's widgets (label, button, entry fields, etc.) developed directly by Java Swing Package.
|Functionality and Implementation
||Java AWT many features that are completely developed by the developer. It serves as a thin layer of development on the top of the OS.
||Swing components provide the higher-level inbuilt functions for the developer that facilitates the coder to write less code.
||Java AWT needs a higher amount of memory for the execution.
||Java Swing needs less memory space as compared to Java AWT.
||Java AWT is slower than swing in terms of performance.
||Java Swing is faster than the AWT.
Java Swing Hierarchy
Java defines the class hierarchy for all the Swing Components, which is shown in the following image.
Java Swing Example
In the following example, we have created a User form by using the swing component classes provided by the javax.swing package. Consider the example.
Java awt Example
To understand the differences between Awt and Swing, we have created the same example in awt as well. Consider the following example.