Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Difference between AWT and Swing with Comparison Chart

|| 0

Difference between AWT and Swing with Comparison Chart
Stechies

Java programmers are often confused with respect to 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 of developing graphical user interface (GUI) objects independent of the platform being used, AWT components are platform-dependent and perform differently on varying platforms.

An important interview question as well, the difference between AWT and Swing provides programmers with the ability of using and implementing GUI in better ways. 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. Read on for a closer look at the nature, characteristics, features, functionality and differences between swing and AWT in java.

AWT vs Swing Comparison Chart

The main differences between Swing and AWT can be best understood with the help of the following comparison chart.

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

Heavyweight in character.

Mostly lightweight in character, 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 object 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 possess 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 very powerful and much more in number. They are represented by lists, scroll panes, tables, tabbed panes, colour choosers, etc.

MVC (Model View Controller)

The MVC(Model View Controller) model is not followed by AWT components wherein the data is represented by the model, 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 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 dialog 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 as well as the other controls have to be drawn and their behaviour has to be implemented separately. The host operating system cannot be relied upon for the purpose of performing these functions.

Functionality

AWT components serve as a thin layer of coding that lies atop the OS.

Swing components are much larger in size 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 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 important 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 graphical user interface (GUI) in the form of a lightweight widget toolkit; the toolkit is packaged with widgets with rich functionality. Swing forms an important 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, colour 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();
    } } 

Key difference between Swing and AWT

An understanding of the key differences between Java AWT and Swing go a long way in helping coders use the most appropriate components as and when needed.

1. Swing presents a more-or-less Java GUI. Swing components utilize AWT for creating an operating system window. Thereafter, 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 in order to enable its functionality.

2. An important difference between AWT and Swing is that AWT is heavyweight while Swing components are lightweight.

3. In case of AWT components, native codes are utilized for the generation of view component 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 case of Swing components in Java, the JVM is responsible for generating the view for components.

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 larger in size 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 primitive 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.


Advertisement
Advertisement
Advertisement