AWT, Swings, Applet

Java 12 min min read Updated: Mar 31, 2026 Intermediate
AWT, Swings, Applet
Intermediate Topic 12 of 14

AWT, Swing, and Applet

Before modern web and mobile interfaces became dominant, Java was widely used for creating desktop graphical applications. For this purpose, Java introduced GUI programming libraries such as AWT and Swing. Java also introduced Applet, which allowed small Java programs to run inside web browsers.

Even though Applets are now outdated and no longer used in modern browsers, AWT and Swing are still important from a learning and interview perspective because they explain the foundation of Java GUI programming.

Key Concept: AWT is Java’s original GUI toolkit, Swing is a more advanced GUI toolkit built on top of AWT, and Applet was a browser-based Java program model that is now obsolete.

What is GUI Programming?

GUI stands for Graphical User Interface. It allows users to interact with a program using visual elements like:

  • windows
  • buttons
  • menus
  • text fields
  • labels
  • checkboxes

Instead of typing only commands in the console, users can interact with visual components.

What is AWT?

AWT stands for Abstract Window Toolkit. It is Java’s original GUI toolkit used for building window-based applications.

It provides classes for creating components like:

  • Frame
  • Button
  • Label
  • TextField
  • Checkbox
  • Menu

AWT belongs mainly to the package:

java java.awt

Why It is Called Abstract Window Toolkit

AWT uses the operating system’s native GUI components. That means Java asks the underlying OS to create actual buttons, windows, and other elements.

Because of this:

  • AWT components are platform-dependent
  • their appearance may differ across operating systems

Basic AWT Components

Some common AWT classes are:

  • Frame → main window
  • Label → text label
  • Button → clickable button
  • TextField → single-line text box
  • TextArea → multi-line text box
  • Checkbox → checkbox option
  • Choice → dropdown menu

AWT Example: Simple Frame

java import java.awt.*; public class Main { public static void main(String[] args) { Frame f = new Frame("AWT Example"); f.setSize(300, 200); f.setVisible(true); } }

Here:

  • Frame creates a window
  • setSize() sets the window size
  • setVisible(true) makes the window appear

AWT Example with Button and Label

java import java.awt.*; public class Main { public static void main(String[] args) { Frame f = new Frame("AWT Demo"); Label l = new Label("Welcome to AWT"); Button b = new Button("Click Me"); l.setBounds(50, 50, 150, 30); b.setBounds(50, 100, 80, 30); f.add(l); f.add(b); f.setSize(300, 200); f.setLayout(null); f.setVisible(true); } }

This creates a window with a label and a button.

Limitations of AWT

  • platform-dependent look and feel
  • limited set of components
  • less flexible compared to Swing
  • native component dependency

What is Swing?

Swing is a more advanced GUI toolkit built on top of AWT. It provides richer components and is mostly platform-independent in appearance.

Swing classes belong mainly to:

java javax.swing

Swing improves AWT by providing:

  • more components
  • better customization
  • lightweight components
  • consistent look across platforms

Why Swing is Better than AWT

  • more powerful and flexible
  • supports richer GUI components
  • less dependent on native OS rendering
  • better user experience

Examples of Swing Components

  • JFrame
  • JLabel
  • JButton
  • JTextField
  • JTextArea
  • JCheckBox
  • JRadioButton
  • JTable
  • JMenuBar
  • JOptionPane

AWT vs Swing Component Names

AWT Swing
Frame JFrame
Label JLabel
Button JButton
TextField JTextField
TextArea JTextArea

Basic Swing Example

java import javax.swing.*; public class Main { public static void main(String[] args) { JFrame f = new JFrame("Swing Example"); f.setSize(300, 200); f.setVisible(true); } }

This creates a simple Swing window.

Swing Example with Label and Button

java import javax.swing.*; public class Main { public static void main(String[] args) { JFrame f = new JFrame("Swing Demo"); JLabel l = new JLabel("Welcome to Swing"); JButton b = new JButton("Click Me"); l.setBounds(50, 50, 150, 30); b.setBounds(50, 100, 100, 30); f.add(l); f.add(b); f.setSize(300, 200); f.setLayout(null); f.setVisible(true); } }

This is similar to the AWT example but uses Swing components.

Event Handling in AWT and Swing

GUI applications respond to user actions such as button clicks, key presses, or mouse movement. This is called event handling.

Java uses event listeners for this.

Button Click Example in Swing

java import javax.swing.*; import java.awt.event.*; public class Main { public static void main(String[] args) { JFrame f = new JFrame("Swing Event"); JButton b = new JButton("Click"); b.setBounds(100, 70, 100, 30); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button clicked"); } }); f.add(b); f.setSize(300, 200); f.setLayout(null); f.setVisible(true); } }

Here:

  • ActionListener listens for button click event
  • actionPerformed() runs when the button is clicked

Common Layout Managers

AWT and Swing use layout managers to arrange components inside containers.

Common layout managers:

  • FlowLayout
  • BorderLayout
  • GridLayout
  • CardLayout
  • BoxLayout

FlowLayout Example

java import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame f = new JFrame("FlowLayout Example"); f.setLayout(new FlowLayout()); f.add(new JButton("One")); f.add(new JButton("Two")); f.add(new JButton("Three")); f.setSize(300, 200); f.setVisible(true); } }

Layout managers are preferred over fixed positions in most real GUI programs.

What is an Applet?

An Applet is a small Java program that was designed to run inside a web browser or applet viewer.

Applets were once used for:

  • small web games
  • animations
  • interactive educational tools
  • browser-based visual programs

However, modern browsers no longer support Java Applets due to security, plugin, and technology changes.

Applet Class

To create an applet, Java programs used to extend the Applet class or sometimes JApplet.

Basic package:

java import java.applet.Applet;

Simple Applet Example

java import java.applet.Applet; import java.awt.Graphics; public class MyApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello Applet", 50, 50); } }

This applet displays text inside the applet area.

Applet Lifecycle Methods

Applets had a specific lifecycle controlled by the browser or applet viewer.

  • init()
  • start()
  • paint(Graphics g)
  • stop()
  • destroy()

1. init()

Called once when the applet is first loaded.

2. start()

Called when the applet becomes active.

3. paint(Graphics g)

Used to draw text, shapes, and images.

4. stop()

Called when the applet is no longer active.

5. destroy()

Called when the applet is removed permanently.

Applet Lifecycle Example

java import java.applet.Applet; import java.awt.Graphics; public class MyApplet extends Applet { public void init() { System.out.println("init()"); } public void start() { System.out.println("start()"); } public void paint(Graphics g) { g.drawString("Applet Life Cycle", 50, 50); } public void stop() { System.out.println("stop()"); } public void destroy() { System.out.println("destroy()"); } }

Limitations of Applets

  • browser plugin dependency
  • security restrictions
  • poor modern browser support
  • obsolete technology in modern development

AWT vs Swing

Point AWT Swing
Full form Abstract Window Toolkit Part of Java Foundation Classes
Component type Heavyweight Lightweight
Platform dependence More platform-dependent More platform-independent
Component richness Limited More advanced
Package java.awt javax.swing

AWT and Swing Relationship

Swing is built on top of AWT. That means Swing uses some AWT concepts and classes internally, especially for event handling and windowing support.

So:

  • AWT is the older foundation
  • Swing is the richer extension

Real-World Use Cases

  • desktop forms
  • simple educational tools
  • old enterprise desktop applications
  • custom utility software

In modern Java development, Swing is still sometimes used for desktop tools, but many new applications prefer JavaFX or web-based frontends.

Common Mistakes

  • confusing AWT and Swing components
  • mixing too many fixed bounds instead of using layout managers
  • forgetting event handling for interactive components
  • assuming Applets are still used in modern browsers
  • not understanding that Swing is built over AWT

Best Practices

  • prefer Swing over AWT for richer GUI applications
  • use layout managers instead of hardcoded positioning when possible
  • keep GUI code organized and readable
  • use event listeners properly for user actions
  • treat Applets as historical knowledge, not modern development choice

Interview-Oriented Points

  • AWT is Java’s original GUI toolkit
  • Swing is a more advanced GUI toolkit built on top of AWT
  • AWT components are heavyweight and depend on native OS components
  • Swing components are lightweight and more flexible
  • Applet is a small Java program that used to run in browsers
  • Applet lifecycle methods are init(), start(), paint(), stop(), and destroy()
  • Modern browsers no longer support Applets

Conclusion

AWT, Swing, and Applet are important parts of Java’s GUI history. AWT introduced basic graphical programming, Swing expanded it with richer and more flexible components, and Applets represented Java’s early browser-based execution model.

Even though Applets are obsolete now, understanding AWT and Swing still helps in learning desktop GUI concepts, event handling, and the evolution of Java UI development.

Quick Summary: AWT is Java’s original GUI toolkit, Swing is a richer and more flexible GUI framework built over AWT, and Applet was an old browser-based Java program model that is no longer used in modern web environments.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators