Introduction to Swing

Site Admin · 11 Sep 2026 · 1 views

What is Swing?

Swing is the successor to AWT. All components are lightweight: they are drawn by Java itself, not the native widget toolkit, so they look identical on every platform and can be styled with a pluggable look-and-feel.

Swing components live in javax.swing and keep an MVC architecture: models hold data (e.g. ButtonModel, TableModel), views render it, and controllers/Actions react to input.

Swing JFC architecture

Swing component overview

First Swing Window

import javax.swing.*;

public class Hello extends JFrame {
    Hello() {
        setTitle("Swing Hello");
        setSize(320, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new JLabel("Hello Swing!", SwingConstants.CENTER));
        setVisible(true);
    }
    public static void main(String[] args) {
        new Hello();
    }
}

Swing splash window

Swing vs AWT

  • Swing is fully drawn by Java (consistent look and feels).
  • Swing components are named with a leading J (JButton, JFrame).
  • JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE) ends the app when closed.
Share this post:

Comments (0)

Please login or register to comment.