KTU OOP Java2024 Scheme • 60M ESE
MODULE 4/Section 2: Swings Fundamentals

Overview of AWT, Introduction to Swing, Key Features & Swing vs AWT

HIGH PRIORITY (Prep Guide)Official Syllabus~13 min study
Official KTU 2024 Syllabus Topics Covered Here
Overview of AWTIntroduction to SwingSwing versus AWTSwing key featuresSwing packages

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.

2. Start From Zero: Borrowing Local Furniture (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)!

Key Takeaway: Swing does NOT replace AWT—it is built on top of AWT's foundation (Component, Container, java.awt.event), eliminating AWT's OS-dependent heavyweight restrictions via lightweight components and Pluggable Look & Feel.

3. Essential Exam Definitions

AWT (Abstract Window Toolkit)
Java's original GUI toolkit (java.awt) whose components (Button, Label, TextField) are heavyweight, delegating rendering directly to native OS peer widgets.
Swing
A rich Java GUI framework (javax.swing) built upon AWT that provides flexible lightweight components written entirely in Java with a Pluggable Look and Feel.
Lightweight Component
A GUI widget written 100% in Java that does NOT map directly to an OS-specific native peer component, enabling consistent cross-platform behavior and lower OS resource overhead.
Pluggable Look and Feel (PLAF)
Swing feature allowing developers or users to dynamically change the visual appearance ('look') and interaction style ('feel')—such as Windows, Metal, Nimbus, or custom themes—at runtime without rewriting application logic.

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

  1. Top-level windows (JFrame) inherit AWT's Frame / Container (heavyweight bridge to the OS window manager).
  2. Inside the window, all controls (JButton, JLabel, JPanel) extend JComponent and are drawn cleanly in Java 2D graphics without native OS peers.
  3. Event handling uses AWT's Delegation Event Model (java.awt.event) alongside Swing events (javax.swing.event).

6. Examination-Ready Diagrams

Diagram 14

AWT and Swing Component Hierarchy

java.lang.Objectjava.awt.Componentjava.awt.ContainerWindow -> Frame / DialogHeavyweight Top-Level ParentsJFrame, JDialog, JWindowTop-Level Containers (Contain JRootPane)javax.swing.JComponentLightweight Swing Controls & JPanelJLabel / JPanelPassive / ContainerJTextComponent-> JTextFieldAbstractButtonJButton, JCheckBox...

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).

Key Components
  • Object -> Component -> Container: Core AWT foundation classes (java.awt).
  • Top-Level Heavyweight Containers: Window -> Frame -> JFrame and Dialog -> JDialog (do NOT extend JComponent).
  • JComponent (Lightweight): Root of Swing controls (JLabel, JPanel, JTextComponent -> JTextField, AbstractButton -> JButton, JToggleButton -> JCheckBox, JRadioButton).
Step-by-Step Flow
  1. java.lang.Object -> java.awt.Component -> java.awt.Container.
  2. From Container, top-level containers branch via Window/Frame/Dialog -> JFrame/JDialog.
  3. From Container, javax.swing.JComponent branches into all lightweight Swing widgets and JPanel.
How to Draw in KTU Exam
  • Draw Object -> Component -> Container at 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

Minimal Swing Application Showing Packages & Pluggable Look and FeelSwingIntroDemo.java
Demonstrates javax.swing lightweight controls inside a JFrame and inspecting installed Swing Look & Feel themes safely.
1import javax.swing.*;
2import java.awt.*;
3
4public 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}
Expected Output
Installed Swing Look & Feel themes: 5
 - Metal
 - Nimbus
 - CDE/Motif
 - GTK+
 - ... (varies by OS)
Keywords:javax.swingJFrameJLabelJButtonSwingUtilities
Common Exam & Coding Mistakes
  • Writing import java.swing.*; instead of import javax.swing.*; (the x in javax is mandatory!).
  • Thinking Swing completely replaced AWT—Swing is built ON TOP of AWT (JComponent extends java.awt.Container).

Comparison Tables

AWT vs Swing (High-Yield KTU Comparison Table)
Comparison FeatureAWT (java.awt)Swing (javax.swing)
Component TypeHeavyweight (relies on native OS peers)Lightweight (written purely in Java; except top-level containers)
Platform DependencyPlatform-dependent look and feelPlatform-independent consistent look across OSs
Look & FeelFixed by OSPluggable Look and Feel (changeable at runtime)
MVC SupportDoes not follow MVC cleanlyFollows Model-Delegate (Separable Model) architecture
Component PrefixButton, Label, TextField, FrameJButton, JLabel, JTextField, JFrame (J prefix)
Packagejava.awtjavax.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 (with x), NOT java.swing.

Memory Techniques & Revision

  • Swing Two Pillars — 'L-P': Lightweight Components + Pluggable Look & Feel.

Common Pitfalls to Avoid

  • Claiming JFrame is 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?