Why swing is considered as lightweight? Create a program of swing?

Why Swing Is Considered Lightweight in Java (With Example Program)

In Java, Swing is called a lightweight GUI toolkit because most of its components are drawn and managed entirely by Java code, not by the operating system’s native widgets. This design makes Swing portable, consistent across platforms, and highly customizable.

Key Reasons Swing Is Lightweight

  • Pure Java Rendering: Swing paints its components using Java 2D. It does not rely on native OS peers for buttons, labels, or text fields.
  • Only Top-Level Containers Are Heavyweight: JFrame, JDialog, and JWindow use native resources, but components like JButton, JLabel, JTextField, etc., are lightweight.
  • Pluggable Look and Feel: You can switch the UI theme at runtime without changing code logic, proving independence from platform-specific widgets.
  • Consistent Behavior Across Platforms: Because Swing does its own painting and event handling, the UI behaves similarly on Windows, Linux, and macOS.
  • Easy Customization: You can override methods like paintComponent to create custom visuals and effects.

AWT vs Swing (Quick Contrast)

  • AWT: Heavyweight components; rely on native peers.
  • Swing: Mostly lightweight components; rendered in Java; richer widgets and better flexibility.

Java Swing Program: Simple Greeting App

This easy Swing program creates a window with a text field and a button. When you click the button, it displays a greeting. It runs on the Event Dispatch Thread (EDT) for thread-safe UI updates.

// File: HelloSwing.java
import javax.swing.*;
import java.awt.*;

public class HelloSwing {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Hello Swing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // Root panel with padding and layout
            JPanel panel = new JPanel(new BorderLayout(8, 8));
            panel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));

            // Top: label + text field
            JPanel top = new JPanel(new BorderLayout(8, 8));
            top.add(new JLabel("Name:"), BorderLayout.WEST);
            JTextField nameField = new JTextField();
            top.add(nameField, BorderLayout.CENTER);

            // Center: button
            JButton greetButton = new JButton("Greet");
            JPanel center = new JPanel();
            center.add(greetButton);

            // Bottom: result label
            JLabel resultLabel = new JLabel("Enter your name and click Greet");

            // Add sections to root panel
            panel.add(top, BorderLayout.NORTH);
            panel.add(center, BorderLayout.CENTER);
            panel.add(resultLabel, BorderLayout.SOUTH);

            // Add action
            greetButton.addActionListener(e -> {
                String name = nameField.getText().trim();
                resultLabel.setText(name.isEmpty() ? "Please enter your name."
                                                   : "Hello, " + name + "!");
            });

            frame.setContentPane(panel);
            frame.setSize(360, 180);
            frame.setLocationRelativeTo(null); // center on screen
            frame.setVisible(true);
        });
    }
}

How to Compile and Run

javac HelloSwing.java
java HelloSwing

What This Demonstrates

  • Creation of a lightweight Swing UI using JFrame, JPanel, JLabel, JTextField, and JButton.
  • Event handling via an ActionListener on the button.
  • EDT usage with SwingUtilities.invokeLater for safe UI updates.

Key Takeaways for BTech CSE (5th Semester)

  • Swing is lightweight because components are rendered and managed in Java rather than by the OS.
  • Only top-level containers are heavyweight; most UI elements are lightweight.
  • Lightweight design enables portability, a pluggable look and feel, and easy customization.