java awt vs swing – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Fri, 27 Nov 2020 17:16:20 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg java awt vs swing – Programmerbay https://programmerbay.com 32 32 Difference between AWT and Swing https://programmerbay.com/difference-between-awt-vs-swing/ https://programmerbay.com/difference-between-awt-vs-swing/#respond Thu, 07 Feb 2019 18:17:53 +0000 https://www.programmerbay.com/?p=2506 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.

swing and 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.

Difference between AWT and Swing in Tabular form?

SwingAWT
Swing components are Lightweight components as they are platform independent and draw GUI without using native functionAWT components are Heavyweight as they are dependent on underlying operating system
Swing components are platform-independentAWT components are platform dependent
Swing is built on the top of AWT and completely developed in JavaAWT is a very fine layer of code that lies over the operating system
Swing comparatively requires less time in terms of executionAWT 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 spaceAWT components require a large amount of memory space
It has richer functionality because of its well-defined structureIt 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-inWhile working with AWT you have to implement many things manually
Swing component comes under javax.swing packageAWT 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 componentsAWT has a lesser number of well-defined components

What is GUI ?

GUI stands for Graphical User Interface. It can be defined as a form of user interface that involves GUI components such as buttons,label, drop down lists and other to which a user interacts with. These GUI components are objects that interact through electronic devices such as mouse or keyboard.
GUI Example
Figure : GUI example
In programming context, GUI is primarily for program’s look and feel. In Java, java.awt and javax.swing packages provide classes that allow us to create GUI 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

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.

AWT Example

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.

Java program to show the working AWT

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

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.

Simple Swing Output

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

Java program to show the working Swing

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();
}

}

]]>
https://programmerbay.com/difference-between-awt-vs-swing/feed/ 0