SlideShare a Scribd company logo
11
Events and Applet
JAVA / Adv. OOP
22
Contents
 What is an Event?
 Events and Delegation Event Model
 Event Class, and Listener Interfaces
 Discussion on Adapter Classes
 What, Why, Purpose and benefit
 GUI Discussion
33
Events & Event Handling
 Components (AWT and Swing) generate events in response
to user actions
 Each time a user interacts with a component an event is
generated, e.g.:
 A button is pressed
 A menu item is selected
 A window is resized
 A key is pressed
 An event is an object that describes some state change in a
source
 An event informs the program about the action that must be
performed.
 The program must provide event handlers to catch and
process events. Unprocessed events are passed up
through the event hierarchy and handled by a default (do
nothing) handler
16-16-44
Listener
 Events are captured and processed by “listeners” —
objects equipped to handle a particular type of events.
 Different types of events are processed by different
types of listeners.
 Different types of “listeners” are described as interfaces:
ActionListener
ChangeListener
ItemListener
etc.
16-16-66
Listeners (cont’d)
 Event objects have useful methods. For example,
getSource returns the object that produced this event.
 A MouseEvent has methods getX, getY.
 When implementing an event listener, programmers
often use a private inner class that has access to all the
fields of the surrounding public class.
 An inner class can have constructors and private fields,
like any other class.
 A private inner class is accessible only in its outer class.
88
The Delegation Event Model
 The model provides a standard
mechanism for a source to
generate an event and send it to
a set of listeners
 A source generates events.
 Three responsibilities of a
source:
 To provide methods that allow
listeners to register and unregister
for notifications about a specific type
of event
 To generate the event
 To send the event to all registered
listeners.
Source
Listener
Listener
Listener
events
container
99
The Delegation Event Model
 A listener receives event notifications.
 Three responsibilities of a listener:
 To register to receive notifications about specific events by
calling appropriate registration method of the source.
 To implement an interface to receive events of that type.
 To unregister if it no longer wants to receive those notifications.
 The delegation event model:
 A source multicasts an event to a set of listeners.
 The listeners implement an interface to receive notifications
about that type of event.
 In effect, the source delegates the processing of the event to
one or more listeners.
1010
Event Classes Hierarchy
Object
EventObject
AWTEvent
ActionEvent ComponentEvent ItemEvent TextEvent
FocusEvent InputEvent WindowEvent
KeyEvent MouseEvent
1212
AWT Event Classes and Listener
Interfaces
Event Class Listener Interface
ActionEvent ActionListener
AdjustmentEvent AdjustmentListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
ItemEvent ItemListener
KeyEvent KeyListener
MouseEvent MouseListener, MouseMotionListener
TextEvent TextListener
WindowEvent WindowListener
1313
Semantic Event Listener
 The semantic events relate to operations on the components in the
GUI. There are 3semantic event classes.
 An ActionEvent is generated when there was an action performed on a component
such as clicking on a menu item or a button.
― Produced by Objects of Type: Buttons, Menus, Text
 An ItemEvent occurs when a component is selected or deselected.
― Produced by Objects of Type: Buttons, Menus
 An AdjustmentEvent is produced when an adjustable object, such as a scrollbar, is
adjusted.
― Produced by Objects of Type: Scrollbar
 Semantic Event Listeners
 Listener Interface: ActionListener, Method: void actionPerformed(ActionEvent e)
 Listener Interface: ItemListener, Method: void itemStateChanged (ItemEvent e)
 Listener Interface: AdjustmentListener, Method: void adjustmentValueChanged
(AdjustmentEvent e)
1414
Using the ActionListener
 Stages for Event Handling by ActionListener
 First, import event class
import java.awt.event.*;
 Define an overriding class of event type (implements
ActionListener)
 Create an event listener object
ButtonListener bt = new ButtonListener();
 Register the event listener object
b1 = new Button(“Show”);
b1.addActionListener(bt);
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
/ / Write what to be done. . .
label.setText(“Hello World!”);
}
} addActionListener
ButtonListener
action
Button Click
Event
①
②
two ways to do the same thing.
 Inner class method
 Using an anonymous inner class, you will typically use
an ActionListener class in the following manner:
ActionListener listener = new ActionListener({
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Event happened");
}
1616
OR
 Interface Method
 Implement the Listener interface in a high-level
class and use the actionPerformed method.
public class MyClass extends JFrame implements ActionListener {
...
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Event happened");
}….
}
1717
1818
MouseListener (Low-Level
Listener)
 The MouseListener interface
defines the methods to
receive mouse events.
 mouseClicked(MouseEvent me)
 mouseEntered(MouseEvent me)
 mouseExited(MouseEvent me)
 mousePressed(MouseEvent me)
 A listener must register to
receive mouse events
import javax.swing.*;
import java.awt.event.*;
class ABC extends JFrame implements MouseListener {
ABC()
{
super("Example: MouseListener");
addMouseListener(this);
setSize(250,100);
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
System.out.println(me);
}
public static void main(String[] args) {
new ABC().setVisible(true);
}
}
MouseEvent
Mouse click
Adapter Classes
 Java Language rule: implement all the methods of an interface
 That way someone who wants to implement the interface but does not want or
does not know how to implement all methods, can use the adapter class
instead, and only override the methods he is interested in.
i.e.
 An adapter classes provide empty implementation of all methods
declared in an EventListener interface.
1919
2020
Adapter Classes (Low-Level Event Listener)
 The following classes show examples of listener and adapter pairs:
 package java.awt.event
 ComponentListener / ComponentAdapter
 ContainerListener / ContainerAdapter
 FocusListener / FocusAdapter
 HierarchyBoundsListener /HierarchyBoundsAdapter
 KeyListener /KeyAdapter
 MouseListener /MouseAdapter
 MouseMotionListener /MouseMotionAdapter
 WindowListener /WindowAdapter
 package java.awt.dnd
 DragSourceListener /DragSourceAdapter
 DragTargetListener /DragTargetAdapter
 package javax.swing.event
 InternalFrameListener /InternalFrameAdapter
 MouseInputListener /MouseInputAdapter
Listener Interface Style
2121
public class GoodJButtonSubclass extends JButton implements MouseListener { ...
public void mouseClicked(MouseEvent mouseEvent) { // Do nothing }
public void mouseEntered(MouseEvent mouseEvent) { // Do nothing }
public void mouseExited(MouseEvent mouseEvent) { // Do nothing }
public void mousePressed(MouseEvent mouseEvent) {
System.out.println("I'm pressed: " + mouseEvent);
}
public void mouseReleased(MouseEvent mouseEvent) { // Do nothing } ...
addMouseListener(this); ... }
Listener Inner Class Style
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent mouseEvent) { System.out.println("I'm
clicked: " + mouseEvent); }
public void mouseEntered(MouseEvent mouseEvent) { System.out.println("I'm
entered: " + mouseEvent); }
public void mouseExited(MouseEvent mouseEvent) { System.out.println("I'm
exited: " + mouseEvent); }
public void mousePressed(MouseEvent mouseEvent) { System.out.println("I'm
pressed: " + mouseEvent); }
public void mouseReleased(MouseEvent mouseEvent) { System.out.println("I'm
released: " + mouseEvent); }
};
2222
Using Adapter Inner Class
2323
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
System.out.println("I'm pressed: " + mouseEvent);
}
public void mouseReleased(MouseEvent mouseEvent)
{ System.out.println("I'm released: " + mouseEvent);
}
};
Extend inner class
from Adapter Class
2424
import java.awt.*;
import java.awt.event.*;
public class FocusAdapterExample {
Label label;
public FocusAdapterExample() {
Frame frame = new Frame();
Button Okay = new Button("Okay");
Button Cancel = new Button("Cancel");
Okay.addFocusListener(new MyFocusListener());
Cancel.addFocusListener(new MyFocusListener());
frame.add(Okay, BorderLayout.NORTH);
frame.add(Cancel, BorderLayout.SOUTH);
label = new Label();
frame.add(label, BorderLayout.CENTER);
frame.setSize(450, 400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public class MyFocusListener extends FocusAdapter {
public void focusGained(FocusEvent fe) {
Button button = (Button) fe.getSource();
label.setText(button.getLabel());
}
}
public static void main(String[] args) {
FocusAdapterExample fc = new FocusAdapterExample();
}
}
Ad

More Related Content

What's hot (20)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
C#.NET
C#.NETC#.NET
C#.NET
gurchet
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Corba concepts & corba architecture
Corba concepts & corba architectureCorba concepts & corba architecture
Corba concepts & corba architecture
nupurmakhija1211
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
Nilesh Dalvi
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
DivyaKS12
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
Rokonuzzaman Rony
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
Sangharsh agarwal
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
Haitham El-Ghareeb
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
Greg Sohl
 
Water jug problem ai part 6
Water jug problem ai part 6Water jug problem ai part 6
Water jug problem ai part 6
Kirti Verma
 
Distributed operating system
Distributed operating systemDistributed operating system
Distributed operating system
Prankit Mishra
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
oop Lecture 10
oop Lecture 10oop Lecture 10
oop Lecture 10
Anwar Ul Haq
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Corba concepts & corba architecture
Corba concepts & corba architectureCorba concepts & corba architecture
Corba concepts & corba architecture
nupurmakhija1211
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
Nilesh Dalvi
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
DivyaKS12
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
Rokonuzzaman Rony
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
Haitham El-Ghareeb
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
Greg Sohl
 
Water jug problem ai part 6
Water jug problem ai part 6Water jug problem ai part 6
Water jug problem ai part 6
Kirti Verma
 
Distributed operating system
Distributed operating systemDistributed operating system
Distributed operating system
Prankit Mishra
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
Mukesh Chinta
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 

Similar to Java gui event (20)

Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
arnold 7490
 
Event handling
Event handlingEvent handling
Event handling
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
Amol Gaikwad
 
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfnJAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
KGowtham16
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
Srajan Shukla
 
Advance java programming- Event handling
Advance java programming- Event handlingAdvance java programming- Event handling
Advance java programming- Event handling
vidyamali4
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
Ayesha Kanwal
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
Ankit Dubey
 
What is Event
What is EventWhat is Event
What is Event
Asmita Prasad
 
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWTEVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindiappsdevelopment
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
jammiashok123
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
PRN USM
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
Good657694
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
Amol Gaikwad
 
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfnJAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
KGowtham16
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
Srajan Shukla
 
Advance java programming- Event handling
Advance java programming- Event handlingAdvance java programming- Event handling
Advance java programming- Event handling
vidyamali4
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
Ayesha Kanwal
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
Ankit Dubey
 
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWTEVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindiappsdevelopment
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
jammiashok123
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
PRN USM
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
Good657694
 
Ad

Recently uploaded (20)

How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Ad

Java gui event

  • 2. 22 Contents  What is an Event?  Events and Delegation Event Model  Event Class, and Listener Interfaces  Discussion on Adapter Classes  What, Why, Purpose and benefit  GUI Discussion
  • 3. 33 Events & Event Handling  Components (AWT and Swing) generate events in response to user actions  Each time a user interacts with a component an event is generated, e.g.:  A button is pressed  A menu item is selected  A window is resized  A key is pressed  An event is an object that describes some state change in a source  An event informs the program about the action that must be performed.  The program must provide event handlers to catch and process events. Unprocessed events are passed up through the event hierarchy and handled by a default (do nothing) handler
  • 4. 16-16-44 Listener  Events are captured and processed by “listeners” — objects equipped to handle a particular type of events.  Different types of events are processed by different types of listeners.  Different types of “listeners” are described as interfaces: ActionListener ChangeListener ItemListener etc.
  • 5. 16-16-66 Listeners (cont’d)  Event objects have useful methods. For example, getSource returns the object that produced this event.  A MouseEvent has methods getX, getY.  When implementing an event listener, programmers often use a private inner class that has access to all the fields of the surrounding public class.  An inner class can have constructors and private fields, like any other class.  A private inner class is accessible only in its outer class.
  • 6. 88 The Delegation Event Model  The model provides a standard mechanism for a source to generate an event and send it to a set of listeners  A source generates events.  Three responsibilities of a source:  To provide methods that allow listeners to register and unregister for notifications about a specific type of event  To generate the event  To send the event to all registered listeners. Source Listener Listener Listener events container
  • 7. 99 The Delegation Event Model  A listener receives event notifications.  Three responsibilities of a listener:  To register to receive notifications about specific events by calling appropriate registration method of the source.  To implement an interface to receive events of that type.  To unregister if it no longer wants to receive those notifications.  The delegation event model:  A source multicasts an event to a set of listeners.  The listeners implement an interface to receive notifications about that type of event.  In effect, the source delegates the processing of the event to one or more listeners.
  • 8. 1010 Event Classes Hierarchy Object EventObject AWTEvent ActionEvent ComponentEvent ItemEvent TextEvent FocusEvent InputEvent WindowEvent KeyEvent MouseEvent
  • 9. 1212 AWT Event Classes and Listener Interfaces Event Class Listener Interface ActionEvent ActionListener AdjustmentEvent AdjustmentListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener ItemEvent ItemListener KeyEvent KeyListener MouseEvent MouseListener, MouseMotionListener TextEvent TextListener WindowEvent WindowListener
  • 10. 1313 Semantic Event Listener  The semantic events relate to operations on the components in the GUI. There are 3semantic event classes.  An ActionEvent is generated when there was an action performed on a component such as clicking on a menu item or a button. ― Produced by Objects of Type: Buttons, Menus, Text  An ItemEvent occurs when a component is selected or deselected. ― Produced by Objects of Type: Buttons, Menus  An AdjustmentEvent is produced when an adjustable object, such as a scrollbar, is adjusted. ― Produced by Objects of Type: Scrollbar  Semantic Event Listeners  Listener Interface: ActionListener, Method: void actionPerformed(ActionEvent e)  Listener Interface: ItemListener, Method: void itemStateChanged (ItemEvent e)  Listener Interface: AdjustmentListener, Method: void adjustmentValueChanged (AdjustmentEvent e)
  • 11. 1414 Using the ActionListener  Stages for Event Handling by ActionListener  First, import event class import java.awt.event.*;  Define an overriding class of event type (implements ActionListener)  Create an event listener object ButtonListener bt = new ButtonListener();  Register the event listener object b1 = new Button(“Show”); b1.addActionListener(bt); class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { / / Write what to be done. . . label.setText(“Hello World!”); } } addActionListener ButtonListener action Button Click Event ① ②
  • 12. two ways to do the same thing.  Inner class method  Using an anonymous inner class, you will typically use an ActionListener class in the following manner: ActionListener listener = new ActionListener({ public void actionPerformed(ActionEvent actionEvent) { System.out.println("Event happened"); } 1616
  • 13. OR  Interface Method  Implement the Listener interface in a high-level class and use the actionPerformed method. public class MyClass extends JFrame implements ActionListener { ... public void actionPerformed(ActionEvent actionEvent) { System.out.println("Event happened"); }…. } 1717
  • 14. 1818 MouseListener (Low-Level Listener)  The MouseListener interface defines the methods to receive mouse events.  mouseClicked(MouseEvent me)  mouseEntered(MouseEvent me)  mouseExited(MouseEvent me)  mousePressed(MouseEvent me)  A listener must register to receive mouse events import javax.swing.*; import java.awt.event.*; class ABC extends JFrame implements MouseListener { ABC() { super("Example: MouseListener"); addMouseListener(this); setSize(250,100); } public void mouseClicked(MouseEvent me) { } public void mouseEntered(MouseEvent me) { } public void mouseExited(MouseEvent me) { } public void mousePressed(MouseEvent me) { } public void mouseReleased(MouseEvent me) { System.out.println(me); } public static void main(String[] args) { new ABC().setVisible(true); } } MouseEvent Mouse click
  • 15. Adapter Classes  Java Language rule: implement all the methods of an interface  That way someone who wants to implement the interface but does not want or does not know how to implement all methods, can use the adapter class instead, and only override the methods he is interested in. i.e.  An adapter classes provide empty implementation of all methods declared in an EventListener interface. 1919
  • 16. 2020 Adapter Classes (Low-Level Event Listener)  The following classes show examples of listener and adapter pairs:  package java.awt.event  ComponentListener / ComponentAdapter  ContainerListener / ContainerAdapter  FocusListener / FocusAdapter  HierarchyBoundsListener /HierarchyBoundsAdapter  KeyListener /KeyAdapter  MouseListener /MouseAdapter  MouseMotionListener /MouseMotionAdapter  WindowListener /WindowAdapter  package java.awt.dnd  DragSourceListener /DragSourceAdapter  DragTargetListener /DragTargetAdapter  package javax.swing.event  InternalFrameListener /InternalFrameAdapter  MouseInputListener /MouseInputAdapter
  • 17. Listener Interface Style 2121 public class GoodJButtonSubclass extends JButton implements MouseListener { ... public void mouseClicked(MouseEvent mouseEvent) { // Do nothing } public void mouseEntered(MouseEvent mouseEvent) { // Do nothing } public void mouseExited(MouseEvent mouseEvent) { // Do nothing } public void mousePressed(MouseEvent mouseEvent) { System.out.println("I'm pressed: " + mouseEvent); } public void mouseReleased(MouseEvent mouseEvent) { // Do nothing } ... addMouseListener(this); ... }
  • 18. Listener Inner Class Style MouseListener mouseListener = new MouseListener() { public void mouseClicked(MouseEvent mouseEvent) { System.out.println("I'm clicked: " + mouseEvent); } public void mouseEntered(MouseEvent mouseEvent) { System.out.println("I'm entered: " + mouseEvent); } public void mouseExited(MouseEvent mouseEvent) { System.out.println("I'm exited: " + mouseEvent); } public void mousePressed(MouseEvent mouseEvent) { System.out.println("I'm pressed: " + mouseEvent); } public void mouseReleased(MouseEvent mouseEvent) { System.out.println("I'm released: " + mouseEvent); } }; 2222
  • 19. Using Adapter Inner Class 2323 MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { System.out.println("I'm pressed: " + mouseEvent); } public void mouseReleased(MouseEvent mouseEvent) { System.out.println("I'm released: " + mouseEvent); } };
  • 20. Extend inner class from Adapter Class 2424 import java.awt.*; import java.awt.event.*; public class FocusAdapterExample { Label label; public FocusAdapterExample() { Frame frame = new Frame(); Button Okay = new Button("Okay"); Button Cancel = new Button("Cancel"); Okay.addFocusListener(new MyFocusListener()); Cancel.addFocusListener(new MyFocusListener()); frame.add(Okay, BorderLayout.NORTH); frame.add(Cancel, BorderLayout.SOUTH); label = new Label(); frame.add(label, BorderLayout.CENTER); frame.setSize(450, 400); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } public class MyFocusListener extends FocusAdapter { public void focusGained(FocusEvent fe) { Button button = (Button) fe.getSource(); label.setText(button.getLabel()); } } public static void main(String[] args) { FocusAdapterExample fc = new FocusAdapterExample(); } }

Editor's Notes

  • #5: A listener is an object.
  • #6: ActionListener specifies one method: void actionPerformed(ActionEvent e);
  • #7: Inner classes are not in the AP subset.
  • #8: Inner classes contradict the main OOP philosophy.