In this article, we will go through the meaning of AWT, Swing, GUI and lastly look into the difference between AWT and Swing.
AWT and Swing both are GUI frameworks in Java. Swing is a widely used framework because it offers flexibility and richer GUI framework than AWT.
However, AWT is also an essential part of Java as Swing directly or indirectly depends on AWT classes which apparently means AWT is still needed to use Swing.
Java’s other GUI framework JavaFX would certainly replace Swing in the near future and at that time Swing will be seldomly used to make GUIs.
Swing | AWT |
---|---|
Swing components are Lightweight components as they are platform independent and draw GUI without using native function | AWT components are Heavyweight as they are dependent on underlying operating system |
Swing components are platform-independent | AWT components are platform dependent |
Swing is built on the top of AWT and completely developed in Java | AWT is a very fine layer of code that lies over the operating system |
Swing comparatively requires less time in terms of execution | AWT requires more time in terms of execution |
Swing supports MVC(Modern View Control) | AWT does not support MVC (Modern View Control) |
Swing components do not require any memory space | AWT components require a large amount of memory space |
It has richer functionality because of its well-defined structure | It has no richer functionality because of its dependent code |
Swing has many advanced features like JButton, JDialog, JTextArea, etc. Its components are not native to any Operating System, which in turn provides swing components with those lightweight features | These features are not available in AWT |
While working with Swing most of the functionalities are already built-in | While working with AWT you have to implement many things manually |
Swing component comes under javax.swing package | AWT components comes under java.awt package |
Swing has modernized and illustrative graphical user interface (GUI) | AWT has an old and unappealing graphical user interface (GUI) |
Swing has a higher number of well-defined components | AWT has a lesser number of well-defined components |
Further, these classes can be classified into two categories, GUI classes & Non-GUI support classes. For example, JRadioButton, JFrame are GUI classes, whereas Layout manager classes are Non-GUI support classes.
How user interacts with GUI ?
When a user performs some actions, such as button click, then it is considered as an event. An event is a change in the state of a particular GUI component or object. In button click, ‘click’ is the event and GUI component is the button which is controlled by an event handler that is responsible for deciding what to do after its occurrence.
AWT stands for Abstract Window Toolkit which was interoduced in first release of JDK. It is responsible for handling all the user-interface and graphics tools.
It is the part of the JFC (Java Foundation Class), which is the standardised API for Graphical user-interface for any program. AWT ensures that your program will be well interpreted by the user.
Figure : A simple AWT program output example
AWT components are directly dependent on underlying Operating System. Therefore, these components use the capabilities of that very platform’s Graphical User Interface.
As a result, Java GUI program completely look different while running from one platform to another. In other words, a button component in Windows would not be look similar as in Linux because AWT uses OS native functions to draw those GUI component.
AWT components are often considered as Heavyweight components and take more time to run.
AWT provides a special component that is container class which manages the layout of the components. It also offers wide range of GUI objects such as Button, Checkboxes, Radio Buttons, Labels and more.
Program:
test.html
<html> <head> <title>test</title> </head> <applet code="SwingAndApplet.class" width="200" height="400"></applet> </html>
SwingAndApplet.java
import java.applet.Applet; import java.awt.*;
public class SwingAndApplet extends Applet {
public void paint(Graphics g){
g.drawString(“This is applet example”,100,100);
}
}
Swing is the advanced and modern toolkit for Java. It is also a part of the JFC (Java Foundation Class). It has more personalized and sophisticated features over the earlier Abstract Window Toolkit (AWT).
It emulates a better look and feels for your program. The swing components provide a great look and feel for the program, whereas; AWT does not support this feature.
Figure : A simple Swing program output example
Swing components are pure Java components as they are written in Java. Unlike AWT, Swing offers uniform look and feel across various platforms.
It provides a set of rich lightweight components that includes JButton, JCheckBox, JComboBox,JMenu, JSlider and many more. It sits on the top of AWT API and supports wide range of widgets for developing window based applications
Program:
import javax.swing.*;
public class SwingAndApplet extends JFrame{
JTextArea textArea;
SwingAndApplet()
{
textArea = new JTextArea(“This is Swing example”);
add(textArea);
setBounds(10,30, 300,300);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new SwingAndApplet();
}
}
This post was last modified on November 27, 2020