Java Swing
Framework
A Comprehensive Overview of GUI Development
Name: Hasim Khan
Class: BTech (CSE)
Roll no: 23
Rid: R44078
Submitted to: Mr.
Sachin Sir
Introduction to Java Swing
Java Swing is a part of the Java Foundation
Classes (JFC) used to create window-based
applications.
Built on top of the Abstract Window Toolkit (AWT)
API.
Entirely written in Java, making it platform-
independent.
Provides a rich set of lightweight components
compared to AWT.
Follows the Model-View-Controller (MVC)
architectural pattern.
Key Features
Lightweight Platform Pluggable Look &
Independent Feel
Swing components are
lightweight because they do Write once, run anywhere. Allows the application's
not rely on the native OS's Swing applications look and appearance to change
GUI widgets. They are behave consistently across dynamically at runtime
rendered purely in Java. Windows, MacOS, and Linux. without changing the
underlying code logic.
AWT vs. Swing
Feature Java AWT Java Swing
Nature Heavyweight components Lightweight components
Platform Dependency Platform Dependent (Uses OS peers) Platform Independent (Pure Java)
Speed Faster (uses native OS code) Slower (drawn by Java)
Look and Feel Fixed (OS Style) Pluggable (Customizable)
MVC Support No Yes
Component Hierarchy
Understanding the inheritance is crucial in Swing:
Component: The root class for all visual elements.
Container: Can hold other components.
JComponent: The base class for all Swing
components (buttons, labels, etc.).
Top-Level Containers: Windows that hold
everything else (e.g., JFrame, JDialog).
Top-Level Containers
JFrame JDialog
A frame is a top-level window with a title and a A dialog is a top-level window used to take
border. It is the main container for most Swing some form of input from the user. It is often
applications. It has maximize, minimize, and used for pop-up windows or alerts and can be
close buttons. modal or non-modal.
Note: Every Swing application must have at least one top-level container.
Basic Components
JButton: A push button that performs an action
when clicked.
JLabel: A display area for a short text string or an
image. Read-only for the user.
JTextField: Allows editing of a single line of text. JCheckBox
JTextArea: Allows editing of multiple lines of text.
JCheckBox: An item that can be selected or
deselected, displaying state to the user.
Advanced
Components
Complex Data Handling
Swing provides sophisticated components for
displaying complex data structures.
JTable: Displays data in a two-dimensional grid of
rows and columns, similar to a spreadsheet.
JTree: Displays hierarchical data in a tree structure
(like a file explorer).
JTabbedPane: Allows multiple components to
Layout Managers
Layout managers control the size and position of
components in a container.
BorderLayout: Arranges components in five
regions: North, South, East, West, and Center.
(Default for JFrame).
FlowLayout: Arranges components in a line, one
after another (Default for JPanel).
GridLayout: Arranges components in a
rectangular grid of equal sizes.
GridBagLayout: The most flexible and complex
layout manager.
Event Handling
Delegation Event Model
Swing uses the Delegation Event Model to handle user
interactions.
Source: The component that generates the event
(e.g., Button).
Event: The object that describes the state change
(e.g., ActionEvent).
Listener: The interface that receives and
processes the event (e.g., ActionListener).
Hello World Example
Code Breakdown
1. Import: [Link].* package.
2. Frame: Create a JFrame object.
3. Component: Create a JButton.
4. Add: Add the button to the frame.
5. Visible: Set size and visibility.
Conclusion
Java Swing remains a powerful toolkit for building desktop
applications. Its platform independence, rich set of components,
and customizable look and feel make it an essential skill for Java
developers.
Robus Flexibl Standar
t e d
Q&
A
Any Questions?
Thank you for your
attention!