Overview of AWT, Introduction to Swing, Key Features & Swing vs AWT
1. Topic Introduction
Before Java introduced Swing in J2SE 1.2 (1998), desktop graphical user interfaces (GUIs) relied solely on AWT (Abstract Window Toolkit). Understanding why AWT fell short (heavyweight native OS peers) and how Swing fixed those limitations (lightweight components + pluggable look-and-feel) gives you guaranteed marks in Module 4.
AWT) vs Carrying Custom Portable Furniture (Swing)Imagine building a stage play in 3 countries. With AWT, you ask each local city hall (OS) to lend you their local wooden chairs (heavyweight native peers). Your stage looks different in Windows, Linux, and macOS, and heavy OS rules restrict customization! With Swing, your crew paints and builds every button and label themselves in pure Java (lightweight components)—so your app looks 100% consistent everywhere, AND you can swap themes dynamically (Pluggable Look and Feel)!
Component, Container, java.awt.event), eliminating AWT's OS-dependent heavyweight restrictions via lightweight components and Pluggable Look & Feel.3. Essential Exam Definitions
java.awt) whose components (Button, Label, TextField) are heavyweight, delegating rendering directly to native OS peer widgets.javax.swing) built upon AWT that provides flexible lightweight components written entirely in Java with a Pluggable Look and Feel.4. Detailed Technical Explanation & Syntax
1. Limitations of AWT (Why Swing Was Needed)
From your lecture notes, AWT suffered from three key limitations:
- 1. Platform-Defined Look & Feel: A button rendered differently on Windows vs macOS vs Solaris because the OS controlled appearance.
- 2. Fixed Look & Feel: Programmers could not dynamically switch or customize widget skins cleanly.
- 3. Heavyweight Peer Restrictions: Every AWT widget created an underlying OS peer window—consuming heavy native resources, behaving inconsistently across OSs, and always staying opaque/rectangular.
2. Two Key Features of Swing & Important Swing Packages
Swing eliminates these drawbacks via: (1) Lightweight Components (written purely in Java; look and feel controlled by Swing, not OS) and (2) Pluggable Look and Feel (enabled by Swing's Model-Delegate MVC architecture).
Key Packages to Know in Exams: javax.swing (core JFrame, JButton, JLabel, JTextField, JPanel), javax.swing.event (Swing-specific events like CaretEvent), java.awt (layout managers & core AWT ancestors), and java.awt.event (shared Delegation Event Model classes & listeners like ActionEvent, ActionListener).
5. Working Principle: How Swing Builds on AWT Without Replacing It
- Top-level windows (
JFrame) inherit AWT'sFrame/Container(heavyweight bridge to the OS window manager). - Inside the window, all controls (
JButton,JLabel,JPanel) extendJComponentand are drawn cleanly in Java 2D graphics without native OS peers. - Event handling uses AWT's Delegation Event Model (
java.awt.event) alongside Swing events (javax.swing.event).
6. Examination-Ready Diagrams
AWT and Swing Component Hierarchy
Class inheritance tree showing how Swing builds upon AWT's Component and Container while splitting lightweight JComponent controls from heavyweight top-level containers (JFrame, JDialog).
- Object -> Component -> Container: Core AWT foundation classes (
java.awt). - Top-Level Heavyweight Containers:
Window->Frame->JFrameandDialog->JDialog(do NOT extendJComponent). - JComponent (Lightweight): Root of Swing controls (
JLabel,JPanel,JTextComponent -> JTextField,AbstractButton -> JButton, JToggleButton -> JCheckBox, JRadioButton).
java.lang.Object->java.awt.Component->java.awt.Container.- From
Container, top-level containers branch viaWindow/Frame/Dialog->JFrame/JDialog. - From
Container,javax.swing.JComponentbranches into all lightweight Swing widgets andJPanel.
- Draw
Object->Component->Containerat the top. - Branch left to
Window->Frame->JFrame(mark Heavyweight Top-Level). - Branch right to
JComponent(mark Lightweight) ->JPanel,JLabel,JTextComponent -> JTextField,AbstractButton -> JButton, JToggleButton -> JCheckBox, JRadioButton.
7. Complete Java Code Examples & Tracing
javax.swing lightweight controls inside a JFrame and inspecting installed Swing Look & Feel themes safely.| 1 | import javax.swing.*; |
| 2 | import java.awt.*; |
| 3 | |
| 4 | public class SwingIntroDemo { |
| 5 | public static void main(String[] args) { |
| 6 | // List available Pluggable Look & Feel themes installed in this JDK |
| 7 | UIManager.LookAndFeelInfo[] themes = UIManager.getInstalledLookAndFeels(); |
| 8 | System.out.println("Installed Swing Look & Feel themes: " + themes.length); |
| 9 | for (UIManager.LookAndFeelInfo info : themes) { |
| 10 | System.out.println(" - " + info.getName()); |
| 11 | } |
| 12 | |
| 13 | // Standard Swing creation pattern via EDT (not shown on headless CLI) |
| 14 | if (!GraphicsEnvironment.isHeadless()) { |
| 15 | SwingUtilities.invokeLater(() -> { |
| 16 | JFrame frame = new JFrame("KTU Swing Intro"); |
| 17 | frame.setLayout(new FlowLayout()); |
| 18 | frame.add(new JLabel("Lightweight Swing Component")); |
| 19 | frame.add(new JButton("Click Me")); |
| 20 | frame.setSize(320, 140); |
| 21 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 22 | frame.setVisible(true); |
| 23 | }); |
| 24 | } |
| 25 | } |
| 26 | } |
Installed Swing Look & Feel themes: 5 - Metal - Nimbus - CDE/Motif - GTK+ - ... (varies by OS)
javax.swingJFrameJLabelJButtonSwingUtilities- Writing
import java.swing.*;instead ofimport javax.swing.*;(thexinjavaxis mandatory!). - Thinking Swing completely replaced AWT—Swing is built ON TOP of AWT (
JComponentextendsjava.awt.Container).
Comparison Tables
| Comparison Feature | AWT (java.awt) | Swing (javax.swing) |
|---|---|---|
| Component Type | Heavyweight (relies on native OS peers) | Lightweight (written purely in Java; except top-level containers) |
| Platform Dependency | Platform-dependent look and feel | Platform-independent consistent look across OSs |
| Look & Feel | Fixed by OS | Pluggable Look and Feel (changeable at runtime) |
| MVC Support | Does not follow MVC cleanly | Follows Model-Delegate (Separable Model) architecture |
| Component Prefix | Button, Label, TextField, Frame | JButton, JLabel, JTextField, JFrame (J prefix) |
| Package | java.awt | javax.swing |
Real-World Applications
- Desktop IDEs (NetBeans, IntelliJ's Swing UI base) and cross-platform engineering simulators rely on Swing.
Important KTU Exam Points
- Memorize the 6-row AWT vs Swing table + the two key Swing features (Lightweight Components & Pluggable Look and Feel).
- Note the exact package name:
javax.swing(withx), NOTjava.swing.
Memory Techniques & Revision
- Swing Two Pillars — 'L-P': Lightweight Components + Pluggable Look & Feel.
Common Pitfalls to Avoid
- Claiming
JFrameis lightweight — WRONG! Top-level Swing containers (JFrame,JDialog,JWindow,JApplet) are heavyweight!
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: Swing (javax.swing) is built on the foundation of AWT (java.awt) to overcome AWT's platform dependency and heavyweight restrictions.
- Two Key Features: (1) Lightweight Components — written purely in Java without mapping to native OS peers, guaranteeing consistent cross-platform behavior; (2) Pluggable Look and Feel — enables swapping visual themes dynamically at runtime without changing application code.
- Swing vs AWT: AWT is heavyweight, platform-dependent, non-MVC, and uses fixed OS styling; Swing is lightweight, platform-independent, uses Model-Delegate architecture, and prefixes widgets with
J(JButton,JLabel).
Quick Understanding Check
1. Which package contains Swing's core UI controls like JButton and JLabel?