0% found this document useful (0 votes)
11 views31 pages

Java Swing GUI Programming Basics

Chapter 3 discusses GUI and event-driven programming in Java, focusing on the Swing API for desktop applications. It outlines the structure of GUI applications, including components, containers, and event handling mechanisms. The chapter emphasizes the importance of layout managers and event listeners in creating responsive user interfaces.

Uploaded by

Mukesh Periasamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views31 pages

Java Swing GUI Programming Basics

Chapter 3 discusses GUI and event-driven programming in Java, focusing on the Swing API for desktop applications. It outlines the structure of GUI applications, including components, containers, and event handling mechanisms. The chapter emphasizes the importance of layout managers and event listeners in creating responsive user interfaces.

Uploaded by

Mukesh Periasamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 3

GUI and
Event Driven
Programming
Prepared by
Muhammad Shukri Bin Che lah
Java provides multiple
APIs for building
Introduction Graphical User
Interfaces (GUIs)
to GUI
Programming
in Java Three main APIs: AWT,
Swing, JavaFX

Focus: Swing for


desktop GUI
development
AWT: JDK 1.0 - Native-
based, limited UI -
GUI API Mostly obsolete

Comparison
Swing: JDK 1.2 -
Lightweight, rich
components -
Commonly used

JavaFX: JDK 8 -
Modern UI, multimedia
support - Used in newer
apps
Richer components (e.g.,
JTable, JTabbedPane)
Why
Swing? Platform-independent
rendering

Consistent look and feel


across platforms

Easier to design
compared to AWT
GUI Container
and
Elements of Component
GUI
Applications
Event

Listener
Container holds and
organizes GUI
Containers components
and
Components
Component is an
interactive element
(button, label, textfield,
etc.)

Common containers:
JFrame, JPanel
1. Define and set
GUI up components

Creation
Steps 2. Create listener
objects

3. Link
components to
listeners
JFrame - Main Window
Container

 Used as the base window


 Syntax:
JPanel - Sub-container

 Organizes components inside JFrame


 Example:
JButton - Push Button

 Used to trigger an action


 Example:
JLabel - Display Text

 Displays non-editable text


 Example:
JTextField - Text Input

 Single-line input field


 Example:
JCheckBox  JCheckBox allows multiple
selections
and  JRadioButton allows one
JRadioButton selection from a group
JComboBox - Dropdown List

 Displays a list of options


 Example:
JOptionPane - Dialog Box

 Message, input, and confirmation dialogs


 Example:
JMenuBar  Create menus for applications

and  JMenuBar holds JMenu


 JMenu holds JMenuItem
JMenuItem
Layout  FlowLayout

Managers  GridLayout
 BorderLayout
Overview
FlowLayout Example

 Syntax:
GridLayout Example

 Syntax:
BorderLayout Example

 Syntax:
Color and Font Classes

 Set GUI appearance


 Color:

 Font:
Event-Driven
In procedural
Programming programming, code
executes in a fixed
Overview order

In event-driven
programming, code
responds to user or
system events
What is
an Event?

A SIGNAL THAT SOMETHING GENERATED BY USER


HAS HAPPENED ACTIONS (E.G., CLICKS,
TYPING) OR SYSTEM
ACTIONS (E.G., TIMER)
Source
and
Listener
SOURCE: LISTENER: OBJECT
COMPONENT THAT THAT RECEIVES AND
GENERATES AN EVENT HANDLES THE EVENT
Event Event Handler: Code
Handling that performs tasks in
response to an event

and
Handlers Event Handling:
Mechanism that
connects components
and logic
Delegation
Event
Model

JAVA USES TWO OBJECT TYPES:


DELEGATION-BASED EVENT SOURCE AND
MODEL EVENT LISTENER
 JButton - ActionEvent

Event Types  JCheckBox - ItemEvent /


ActionEvent

by  JList - ListSelectionEvent

Component  Window - WindowEvent


 Any component - MouseEvent,
KeyEvent
 ActionEvent - ActionListener -
actionPerformed()

Selected  ItemEvent - ItemListener -


itemStateChanged()

Event  MouseEvent - MouseListener -


mousePressed(), etc.
Handlers  KeyEvent - KeyListener -
keyTyped(), keyPressed(),
keyReleased()
Event
 Event handling code can be
placed:

Handling  - Within the class


 - In an external class
Placement  - As an anonymous inner class
Summary

 Java Swing simplifies GUI


development
 GUI apps involve
components, containers,
events, and listeners
 Layout managers help
organize components
effectively
Thank you

You might also like