Register Login

Difference between AWT and Swing

Updated Jan 15, 2020

Java programmers are often confused about the difference between AWT and Swing components, the features of AWT and Swing, and the functionalities of both.

While Swing is a collection of program components that possess the ability to develop graphical user interface (GUI) objects independent of the platform being used, AWT components are platform-dependent and perform differently on varying platforms.

As this article progresses, readers would understand the definition of Swing and AWT as well as the key differences between the two in the form of a comparison chart and points alike. 

AWT vs. Swing

Basis of Differentiation Java AWT Java Swing
Platform dependency AWT components are dependent on the nature of the platform. Java swing components are not dependent on the nature of the platform. They are purely scripted in Java.
Weightiness A heavyweight in character. Mostly lightweight, swing components are designed to sit atop AWT components and perform their tasks. These components are generally lightweight as they do not need any native OS objects for implementing their functionality. JFrame and Jdialog are considered to be heavyweight as they possess a peer. Therefore, components such as Jbutton, JtextArea, etc. are considered to be lightweight as they do not maintain any OS peer.
Pluggable feel and looks AWT components are non-supportive of the pluggable feel and look. Java swing components offer support to the pluggable feel and look. Java swing API contains classes such as Jbutton, JtextField, JradioButton, Jcheckbox, JtextArea, Jmenu, JcolorChooser, etc. that add further functionality to the objects being drawn.

 

Number of components AWT components are less in number in comparison to Swing components. Java Swing components are compelling and much more in number. They are represented by lists, scroll panes, tables, tabbed panes, color choosers, etc.
MVC (Model View Controller) The MVC(Model View Controller) model is not followed by AWT components wherein the model represents the data, presentation represents the view, and the controller serves as the bridging interface between the view and model. Swing components in Java are known to follow the MVC (Model View Controller), model.
Stands for The AWT full form is the Abstract windows toolkit. Swing components in Java are also referred to as JFC’s (Java Foundation Classes).
Package required AWT components need the java.awt package. Swing components in Java need the javax.swing package.
Advanced features The advanced features depicted by Swing components are not present in AWT components. Swing components depict several advanced features such as Jtabel, Jtabbed pane, etc. These advanced features are specific only to Swing components.
Presence of peers There are 21 “peers” in AWT components. There exists one peer for each control, and there is one peer for the dialogue itself. Peers are in the form of widgets offered by the operating system. They may be a button object or a specific entry field object. Only one peer is present in Swing in the form of the operating system’s window object. The entry fields, buttons, etc. are developed by the Swing package directly onto the drawing surface offered by the window object. Swing possesses more codes because of this very reason. The button and their behavior have to be implemented separately. The host operating system cannot be relied upon to perform these functions.
Functionality AWT components serve as a thin layer of coding that lies atop the OS. Swing components are much larger and offer higher levels of functionality.
Implementation Many features/functions of AWT components have to be implemented by the coder. Swing components provide in-built functions for their performance.
Memory space AWT components require and occupy larger memory space. Swing components do not occupy as much memory space as AWT components.
Speed of execution AWT components are slower than swing components in terms of performance. Swing in Java is faster than AWT in Java.

What is AWT in JAVA

Abstract Window Toolkit (AWT) refers to a collection of application program interfaces (API s) that are utilized by Java programmers for the creation of graphical user interface (GUI) objects. These objects are in the form of scroll bars, windows, buttons, etc. This toolkit is an essential component of Java Foundation Classes (JFC) belonging to Sun Microsystems, which is the company responsible for the origin of Java.

Simple Java AWT Example

//Swaping two Number using Java AWT
package swap;
import java.awt.*;
import java.awt.event.*;

public class swap
{
    public static void main(String args[])
    {
        Frame f = new Frame("My Frame");

        Label l1 = new Label ("First");
        Label l2 = new Label ("Second");

        TextField t1 = new TextField(20);
        TextField t2 = new TextField(20);

        Button b = new Button("OK");

        f.add(l1);
        f.add(t1);
        f.add(l2);
        f.add(t2);
        f.add(b);

        b.addActionListener(new ActionListener () {
            public void actionPerformed(ActionEvent ae)
            {
                String temp = t1.getText();
                t1.setText(t2.getText());
                t2.setText(temp);
            }
        });

        f.layout(new FlowLayout());
        f.setSize(300,300);
        f.setVisible(true);
    }
}

What is Swing in JAVA

Swing in Java refers to a graphical user interface (GUI) in the form of a lightweight widget toolkit; the toolkit is packaged with widgets with rich functionality.

Swing forms an integral part of the Java Foundation Classes (JFC) and provides coders with several useful packages for the development of rich desktop-friendly applications in Java. The built-in controls in Swing comprise of image buttons, trees, tabbed panes, color choosers, sliders, toolbars, tables, etc.

Swing also enables the creation of text areas for displaying rich text format (RTF) or HTTP objects. Swing components are written purely in java and are therefore independent of the platform they work on.

Simple Java Swing Example

//SWAPing

import javax.swing.*;
import java.awt.event.*;

public class swap implements ActionListener{
    JTextField tf1,tf2;
    JButton b1;
    swap(){
        JFrame f= new JFrame();
        tf1=new JTextField();
        tf1.setBounds(50,50,150,20);
        tf2=new JTextField();
        tf2.setBounds(50,100,150,20);
        b1=new JButton("Ok");
        b1.setBounds(50,200,50,50);
        b1.addActionListener(this);
        f.add(tf1);f.add(tf2);f.add(b1);
        f.setSize(300,300);
        f.setLayout(null);
        f.setVisible(true);
    }
     public void actionPerformed(ActionEvent e) {
        String temp = t1.getText();
        t1.setText(t2.getText());
        t2.setText(temp);
    }
    public static void main(String[] args) {
        new swap();
    } }


A key difference between Swing and AWT

  • An understanding of the key differences between Java AWT and Swing goes a long way in helping coders use the most appropriate components as and when needed.
  • Swing presents a more-or-less Java GUI. Swing components utilize AWT for creating an operating system window. After that, Swing draws buttons, labels, checkboxes, text boxes, etc. directly on to the window. These Swing objects are designed to respond to key entries and mouse-clicks alike. Here, users can decide upon what they would like to do rather than allow the OS to handle the features and functionalities to be put to use. On the other hand, AWT being a cross-platform interface uses the OS or native GUI toolkit to enable its functionality.
  • An essential difference between AWT and Swing is that AWT is heavyweight while Swing components are lightweight.
  • In the case of AWT components, native codes are utilized for the generation of view components as provided by the OS. This is the reason behind the look and feel of AWT components changing from one platform to another. In comparison, in the case of Swing components in Java, the JVM is responsible for generating the view for parts.

Conclusion

This tutorial helps users understand the functionality and features of both for better utilization of their components. Swing offers richer functionality, is lightweight, and much more extensive than AWT. Java programmers desirous of accessing built-in functions rather than creating them on their own should opt for Swing rather than AWT.

Additionally, in case the work on hand is GUI intensive, then AWT components would give off a very first look and feel in comparison to their Swing counterparts. This is because Swing implements the GUI functionality on its own rather than depend on the host OS platform for the same – so, choose between these java components accordingly to attain the best results.


×