Lesson: JAVA SWING
Introduction to Swing Package
Interfaces and Event-Driven Programming
Learning Objectives
At the end of the lesson(s), students must be able to:
1. Understand the Swing package in Java and its role in creating graphical user interfaces
(GUIs).
2. Identify key classes and components provided by Swing for building GUI applications.
3. Understand the concept of event-driven programming in Java Swing.
4. Explain the importance of interfaces in handling events.
5. Describe how GUI applications interact with users.
Introduction
When students first learn Java programming, they usually create console-based applications.
These are programs where users type commands using the keyboard and outputs are displayed as
plain text.
Example of a console application:
[Enter your name:]
Although console applications are useful for learning the fundamentals of programming, modern
software applications are usually graphical. Users interact with windows, buttons, menus, icons,
and forms rather than typing commands manually.
This is where Java Swing becomes important.
Java Swing allows programmers to create graphical user interfaces or GUI applications. Through
Swing, Java programs become more interactive, user-friendly, and visually appealing.
Examples of applications that use graphical interfaces include:
• ATM machines
• Enrollment systems
• Facebook login pages
• Music players
• Online shopping systems
• Reservation systems
• Calculator applications
These applications are easier to use because users interact visually with the system.
What is Java Swing?
Java Swing is a package in Java used for creating graphical user interfaces.
Swing is part of the Java Foundation Classes (JFC) and contains classes and components that help
developers build desktop applications.
The Swing package is imported using:
import [Link].*;
The [Link] package provides ready-made components that developers can use to build
applications quickly.
Instead of designing every button or textbox from scratch, Swing already provides these
components.
What is GUI?
GUI stands for Graphical User Interface.
A GUI is a visual way of interacting with a computer application.
Instead of typing commands, users can:
• Click buttons
• Type inside text fields
• Select options
• Open menus
• Interact with windows
GUI applications are more user-friendly because users can easily understand what actions to
perform.
Importance of GUI Applications
GUI applications are important because they improve user experience.
Imagine using Facebook without buttons or images. Users would need to type commands manually
just to send messages or upload photos.
Graphical interfaces simplify interaction between the user and the system.
Benefits of GUI applications:
1. Easier to use
2. More attractive
3. Faster interaction
4. More organized
5. Suitable for ordinary users
Console Applications vs GUI Applications
Console Applications
Characteristics:
• Text-based
• Keyboard input only
• Runs in command prompt or terminal
• Simple appearance
Advantages:
• Easier for beginners
• Consumes less memory
• Faster for small programs
Disadvantages:
• Not visually appealing
• Harder for ordinary users
• Limited interaction
GUI Applications
Characteristics:
• Visual-based
• Uses windows and components
• Supports mouse and keyboard interaction
• More interactive
Advantages:
• User-friendly
• Professional appearance
• Better user experience
Disadvantages:
• Slightly more complex
• Requires understanding of components and events
Why Use Swing?
Swing became popular because it improved Java GUI development.
Advantages of Swing include:
1. Platform Independent
Swing applications can run on different operating systems.
Examples:
• Windows
• Linux
• MacOS
This follows Java’s principle:
“Write Once, Run Anywhere”
2. Rich Set of Components
Swing provides many components such as:
• Buttons
• Labels
• Text fields
• Menus
• Tables
• Checkboxes
• Radio buttons
These components help developers create complete applications.
3. Customizable
Swing components can be customized.
Developers can change:
• Font styles
• Colors
• Sizes
• Layouts
• Backgrounds
This allows applications to look more professional.
4. Event Handling Support
Swing supports event-driven programming.
Applications can respond to user actions such as:
• Clicking buttons
• Typing text
• Selecting options
• Pressing keyboard keys
AWT vs Swing
Before Swing, Java used AWT or Abstract Window Toolkit.
Swing was introduced as an improved version of AWT.
AWT
Characteristics of AWT:
• Older GUI library
• Uses native operating system components
• Limited components
• Less customizable
Package:
import [Link].*;
Swing
Characteristics of Swing:
• Improved version of AWT
• Lightweight components
• More customizable
• More advanced components
• Better appearance
Package:
import [Link].*;
Comparison Between AWT and Swing
AWT Swing
Older technology Improved technology
Limited components More components
Heavyweight Lightweight
Less customizable Highly customizable
Depends on operating system Platform independent
Important Swing Components
Swing provides many GUI components.
Each component has a specific purpose.
JFrame
JFrame is the main window of a Swing application.
It acts as the container that holds all components.
Without a JFrame, there is no visible application window.
Examples of applications using a JFrame:
• Login forms
• Registration systems
• Calculator windows
• Student information systems
Real-Life Analogy of JFrame
Imagine a classroom bulletin board.
The bulletin board serves as the main container where notices and posters are attached.
Similarly, JFrame is the container where buttons, labels, and text fields are placed.
JLabel
A JLabel displays text or images.
Examples:
• Username:
• Password:
• Welcome Student
Labels help guide users by providing instructions or descriptions.
JTextField
A JTextField allows users to input text.
Examples:
• Username field
• Search bar
• Student name input
Text fields are important because they collect information from users.
JButton
A JButton is a clickable button.
Examples:
• Login
• Submit
• Save
• Cancel
Buttons are used to trigger actions.
Without event handling, buttons would not perform any task.
JCheckBox
A JCheckBox allows users to select multiple options.
Example:
[ ] Music
[ ] Sports
[ ] Movies
Users can select more than one option.
JRadioButton
A JRadioButton allows only one selection.
Example:
( ) Male
( ) Female
Only one option can be selected at a time.
JTextArea
A JTextArea is used for multiple lines of text.
Examples:
• Feedback forms
• Comments section
• Notes area
Layout Management
After adding components to a window, developers need to organize their positions.
This process is called layout management.
A layout controls how components are arranged inside the window.
Without proper layout management:
• Components may overlap
• The interface may look messy
• Users may become confused
Common Layout Managers
FlowLayout
Arranges components from left to right.
Similar to how words are arranged in a sentence.
BorderLayout
Divides the window into five sections:
• North
• South
• East
• West
• Center
GridLayout
Arranges components like a table.
Useful for:
• Calculator applications
• Dashboards
• Forms
Null Layout
Allows manual positioning of components.
The programmer controls the exact location and size.
Event-Driven Programming
One of the most important concepts in Swing is event-driven programming.
What is Event-Driven Programming?
Event-driven programming is a programming approach where the program waits for events or user
actions.
When an event happens, the application responds automatically.
Examples of events:
• Clicking a button
• Typing on the keyboard
• Moving the mouse
• Selecting menu items
Real-Life Example of Event-Driven Programming
Imagine an elevator.
The elevator waits for the user to press a button.
Once the button is pressed, the elevator responds by moving to the selected floor.
The elevator reacts only when an event occurs.
Swing applications behave the same way.
The program waits for user interaction before responding.
Event Handling
The process of responding to events is called event handling.
Without event handling:
• Buttons would not work
• Menus would not respond
• Applications would become useless
Event handling makes applications interactive.
Action Events
One of the most common events in Swing is the action event.
Action events usually happen when:
• A button is clicked
• A menu item is selected
Swing uses listeners to detect these events.
ActionListener Interface
The most common interface used in Swing event handling is:
ActionListener
The ActionListener waits for button clicks.
When a button is clicked, the method:
actionPerformed()
is executed.
What is an Interface?
An interface in Java is a structure that contains methods that classes must follow or implement.
Interfaces act like rules or contracts.
They define expected behavior.
Real-Life Analogy of an Interface
Think of a remote control.
Different television brands may exist:
• Samsung
• LG
• Sony
But all televisions follow common remote functions:
• Power
• Volume
• Channel
The remote defines what actions should exist.
Similarly, interfaces define what methods should be implemented.
Importance of Interfaces in Swing
Swing heavily depends on interfaces because interfaces allow components to respond to events.
Common interfaces include:
• ActionListener
• MouseListener
• KeyListener
These interfaces detect different types of user interaction.
How Event-Driven Programming Works in Swing
The process usually works like this:
Step 1
The application creates GUI components.
Step 2
The program waits for user interaction.
Step 3
The user performs an action.
Example:
• Clicking a button
Step 4
The event listener detects the action.
Step 5
The application executes a response.
Examples:
• Displaying a message
• Saving information
• Opening another window
Real-Life Applications of Swing
Swing can be used to create:
• Enrollment systems
• Banking systems
• Inventory systems
• Library systems
• Reservation systems
• Student information systems
Although modern frameworks exist today, Swing remains important for learning GUI development
and event-driven programming concepts.
Best Practices in GUI Design
1. Keep the Interface Simple
Avoid overcrowding the window with too many components.
2. Use Clear Labels
Labels should guide users properly.
3. Organize Components Properly
Good layout improves readability and usability.
4. Use Meaningful Button Names
Examples:
• Save
• Login
• Submit
Avoid unclear labels.
5. Ensure Proper Event Handling
Interactive components should respond correctly to user actions.
Common Beginner Mistakes
1. Forgetting to Display the Frame
If the frame is not displayed, the application window will not appear.
2. Forgetting to Add Components
Components must be added to the JFrame.
3. Poor Layout Management
Improper layouts may cause overlapping components.
4. Missing Event Handling
Buttons without listeners will not perform actions.
Summary
Java Swing is a package used for creating graphical user interfaces in Java.
Swing applications allow users to interact visually using windows, buttons, labels, text fields, and
other components.
Important Swing components include:
• JFrame
• JLabel
• JTextField
• JButton
• JCheckBox
• JRadioButton
• JTextArea
Swing also uses event-driven programming, where the application waits for user actions and
responds automatically.
Interfaces play an important role because they help applications detect and respond to events.
Understanding Swing provides students with a strong foundation in GUI development and
interactive software design.
Activity: Discussion Questions
1. What is Java Swing and why is it important?
2. What is the difference between console applications and GUI applications?
3. Why are GUI applications more user-friendly?
4. What is the purpose of JFrame?
5. What is event-driven programming?
6. Why are interfaces important in Swing?
7. Explain the role of ActionListener.
8. What may happen if an application has poor layout management?
9. Why is event handling important in GUI applications?
10. Give real-life examples of applications that use GUI.