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

MVC & Swing Model-Delegate Architecture, Containers, Root Panes & Component Hierarchy

HIGH PRIORITY (Prep Guide)Official Syllabus~17 min study
Official KTU 2024 Syllabus Topics Covered Here
Model View Controller (MVC)Swing architectureComponents and containersSwing component hierarchy

1. Topic Introduction

How does Swing let you change the look and feel of every button at runtime without altering your event code? And how does Swing organize windows, panels, and menu bars inside a single frame? The answers lie in two cornerstone exam architectures: Swing's Model-Delegate MVC and the Containment & Top-Level Container Panes Hierarchy.

2. Start From Zero: Scoreboard State (Model), Display + Referee (UI Delegate) & Russian Window Frames (Panes)

Every visual control (like a Checkbox) has 3 traits: (1) Model = is it checked or unchecked? (state data); (2) View = how it looks on screen; (3) Controller = what happens when clicked. Classic MVC keeps all 3 separate. Swing noticed View and Controller interact so tightly that it merged them into ONE unit called the UI Delegate (Model-Delegate Architecture), making Pluggable Look & Feel possible! Meanwhile, a window (JFrame) works like a picture frame containing transparent layers (**JRootPane): your buttons go inside the Content Pane, the Layered Pane** stacks depth layers (menu bar + content pane), and the transparent Glass Pane sits on top to intercept window-wide clicks!

Key Takeaway: Swing uses Model-Delegate (Separable Model) architecture: Model (ButtonModel, Document) + UI Delegate (ComponentUI = View + Controller). Every GUI needs a top-level heavyweight container (JFrame), which manages a JRootPane containing Glass, Layered, and Content panes.

3. Essential Exam Definitions

Model-View-Controller (MVC)
Software architecture splitting an interactive component into three concerns: Model (application state/data), View (visual rendering), and Controller (user input handling).
Model-Delegate (Separable Model) Architecture
Swing's adapted version of MVC that combines View and Controller into a single logical entity called the UI Delegate (ComponentUI), enabling Pluggable Look and Feel.
Component vs Container
A Component is an independent visual control (JButton, JLabel); a Container is a specialized component capable of holding other components and nested containers (JFrame, JPanel), forming a Containment Hierarchy.

4. Detailed Technical Explanation & Syntax

1. Classic MVC vs Swing Model-Delegate Architecture

In classic MVC: User clicks checkbox -> Controller updates Model -> View reads Model and repaints. Because View and Controller are tightly coupled in GUI controls, Swing combines them into a UI Delegate inheriting ComponentUI (e.g., ButtonUI), while Models are defined by interfaces (e.g., ButtonModel for buttons, Document for JTextField).

Because the UI Delegate (Look + Feel) is completely decoupled from the Model (State), changing Look & Feel swaps the UI Delegate without touching your Model or business logic!

2. Swing Component Hierarchy & Heavyweight vs Lightweight Containers

Hierarchy Root: java.lang.Object -> java.awt.Component -> java.awt.Container.

Two Container Categories:

  • Top-Level Containers (Heavyweight — do NOT extend JComponent): JFrame, JDialog, JWindow, JApplet. They sit at the very top of every containment hierarchy and bridge Swing to OS windows.
  • Lightweight Containers (extend JComponent): JPanel (most common general-purpose grouping container). Can sit inside a JFrame or inside other JPanels to build organized sub-layouts.

3. Top-Level Container Panes (JRootPane) & Event Dispatching Thread (EDT)

Every top-level container (JFrame) delegates window management to a lightweight JRootPane, which contains three nested panes:

  • 1. Glass Pane: Transparent JPanel sitting on top covering all other panes; captures global mouse events or paints overlays.
  • 2. Layered Pane (JLayeredPane): Gives components a z-order depth value and holds both the optional Menu Bar and the Content Pane.
  • 3. Content Pane: Opaque JPanel where your application adds its visible buttons, labels, and panels (frame.add(...) forwards to the content pane since JDK 5).
  • EDT Rule: Swing is NOT thread-safe across arbitrary threads—all Swing components and updates MUST execute on the Event Dispatching Thread (EDT) using SwingUtilities.invokeLater(Runnable) (asynchronous) or SwingUtilities.invokeAndWait(Runnable) (synchronous).

5. Working Principle: Lifecycle inside Model-Delegate + Containment Hierarchy

  1. 1. main() schedules frame construction onto the EDT via SwingUtilities.invokeLater(...).
  2. 2. JFrame (heavyweight top-level) initializes JRootPane -> JLayeredPane -> Content Pane.
  3. 3. JButton pairs its ButtonModel (pressed/armed state) with a ButtonUI delegate (current Look & Feel renderer/controller).

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.
Diagram 15

MVC & Swing Model-Delegate Architecture

Classic 3-Part MVCModelState & DataViewScreen RenderControllerUser InputSwing Model-Delegate (Separable Model)ModelButtonModel / DocumentUI DelegateView + Controller (ComponentUI)Enables Pluggable Look & Feel

Side-by-side illustration of classic 3-part Model-View-Controller (MVC) and Swing's adapted Model-Delegate (Separable Model) architecture that merges View + Controller into the UI Delegate.

Key Components
  • Model: Stores component state & data (e.g., ButtonModel, Document).
  • View: Visual rendering of the component on screen.
  • Controller: Handles user input (clicks, keys) and updates the Model.
  • UI Delegate (Swing): Combines View + Controller (ComponentUI, e.g., ButtonUI) enabling Pluggable Look and Feel.
Step-by-Step Flow
  1. User interacts with component -> UI Delegate (Controller aspect) captures event.
  2. UI Delegate updates the Model state (e.g., button pressed / checkbox checked).
  3. Model notifies UI Delegate (View aspect) of state change -> UI Delegate repaints component.
How to Draw in KTU Exam
  • Draw Classic MVC triangle (Model, View, Controller) on the left.
  • Draw Swing Model-Delegate on the right: Model (ButtonModel) <---> [UI Delegate (ComponentUI): View + Controller].
  • Explicitly note 'Separable Model / Pluggable Look & Feel' underneath.

7. Complete Java Code Examples & Tracing

Containment Hierarchy (`JFrame -> JPanel -> Controls`) & EDT PatternSwingArchitectureDemo.java
Demonstrates constructing a nested containment hierarchy using JFrame, JPanel, Container content pane, and SwingUtilities.invokeLater.
1import javax.swing.*;
2import java.awt.*;
3
4public class SwingArchitectureDemo {
5 public static void createAndShowGUI() {
6 // 1. Top-level heavyweight container
7 JFrame frame = new JFrame("Swing MVC & Containment Demo");
8 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9
10 // 2. Access Content Pane explicitly
11 Container contentPane = frame.getContentPane();
12 contentPane.setLayout(new BorderLayout(8, 8));
13
14 // 3. Lightweight JPanel grouping sub-components
15 JPanel topPanel = new JPanel(new FlowLayout());
16 JLabel title = new JLabel("Model-Delegate Architecture");
17 JButton actionBtn = new JButton("Inspect Model");
18 topPanel.add(title);
19 topPanel.add(actionBtn);
20
21 contentPane.add(topPanel, BorderLayout.CENTER);
22 frame.setSize(380, 160);
23 frame.setVisible(true);
24 }
25
26 public static void main(String[] args) {
27 // Verify headless-safe check before launching visual window
28 if (GraphicsEnvironment.isHeadless()) {
29 System.out.println("Swing containment hierarchy verified (headless environment).");
30 return;
31 }
32 // Standard KTU pattern: schedule UI creation on Event Dispatching Thread (EDT)
33 SwingUtilities.invokeLater(new Runnable() {
34 @Override
35 public void run() {
36 createAndShowGUI();
37 }
38 });
39 }
40}
Expected Output
Swing containment hierarchy verified (headless environment).
Keywords:JFrameJPanelJComponentSwingUtilitiesinvokeLater
Common Exam & Coding Mistakes
  • Drawing JFrame as a subclass of JComponent in the hierarchy diagram — WRONG! JFrame extends java.awt.Frame, NOT JComponent!
  • Forgetting JRootPane, Glass Pane, Layered Pane, and Content Pane when explaining top-level containers.

Real-World Applications

  • All Swing windows isolate business data models (TableModel, ButtonModel) from theme rendering (ComponentUI).

Important KTU Exam Points

  • Draw BOTH Diagram 14 (Component Hierarchy clearly splitting JFrame from JComponent) AND Diagram 15 (Classic MVC vs Swing Model-Delegate)!
  • Explain why SwingUtilities.invokeLater() is used (runs GUI setup on the Event Dispatching Thread).

Memory Techniques & Revision

  • Root Pane 3 Layers (Top to Bottom) — 'G-L-C': Glass Pane, Layered Pane (holds Menu + Content), Content Pane.
  • Model-Delegate = 'Model + UI Delegate (View + Controller)'.

Common Pitfalls to Avoid

  • Listing JFrame under JComponent in the inheritance tree.

KTU-Style Questions & Exam-Ready Answers

Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.

Introduction: Every Swing GUI control separates state, rendering, and user interaction based on the Model-View-Controller (MVC) paradigm.

  • Classic MVC: Divides a component into Model (state data), View (screen display), and Controller (input response).
  • Swing Model-Delegate (Separable Model): Combines View and Controller into a single logical UI Delegate (ComponentUI, e.g., ButtonUI) separated from the Model interface (ButtonModel, Document).
  • Benefit: Enables Swing's Pluggable Look and Feel—themes can change dynamically without modifying Model or application logic.
Exam Diagram Note: Draw Diagram 15 comparing Classic MVC and Swing Model-Delegate, plus briefly explain ButtonModel + ButtonUI.

Quick Understanding Check

1. In Swing's Model-Delegate architecture, which two MVC components are combined into the UI Delegate?

2. Which Swing container does NOT inherit from JComponent?