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)

IOT Platform Design Methodology
IOT Platform Design Methodology IOT Platform Design Methodology
IOT Platform Design Methodology
poonam kumawat
 
Lecture 1 introduction to vb.net
Lecture 1   introduction to vb.netLecture 1   introduction to vb.net
Lecture 1 introduction to vb.net
MUKALU STEVEN
 
Chapter 5 IoT Design methodologies
Chapter 5 IoT Design methodologiesChapter 5 IoT Design methodologies
Chapter 5 IoT Design methodologies
pavan penugonda
 
Coupling and cohesion
Coupling and cohesionCoupling and cohesion
Coupling and cohesion
Sutha31
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
RTS Tech
 
Database programming
Database programmingDatabase programming
Database programming
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Bayesian networks in AI
Bayesian networks in AIBayesian networks in AI
Bayesian networks in AI
Byoung-Hee Kim
 
Chatbot Abstract
Chatbot AbstractChatbot Abstract
Chatbot Abstract
AntaraBhattacharya12
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ahmed Salama
 
Er model ppt
Er model pptEr model ppt
Er model ppt
Pihu Goel
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
 
INPUT BOX- VBA
INPUT BOX- VBAINPUT BOX- VBA
INPUT BOX- VBA
ViVek Patel
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
Ganga Ram
 
Raspberry Pi Using Python
Raspberry Pi Using PythonRaspberry Pi Using Python
Raspberry Pi Using Python
Seggy Segaran
 
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
IOT DATA MANAGEMENT AND COMPUTE STACK.pptxIOT DATA MANAGEMENT AND COMPUTE STACK.pptx
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
MeghaShree665225
 
House Price Prediction An AI Approach.
House Price Prediction An AI Approach.House Price Prediction An AI Approach.
House Price Prediction An AI Approach.
Nahian Ahmed
 
Housing price prediction
Housing price predictionHousing price prediction
Housing price prediction
Abhimanyu Dwivedi
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
raj upadhyay
 
Explainable AI
Explainable AIExplainable AI
Explainable AI
Dinesh V
 
IOT Platform Design Methodology
IOT Platform Design Methodology IOT Platform Design Methodology
IOT Platform Design Methodology
poonam kumawat
 
Lecture 1 introduction to vb.net
Lecture 1   introduction to vb.netLecture 1   introduction to vb.net
Lecture 1 introduction to vb.net
MUKALU STEVEN
 
Chapter 5 IoT Design methodologies
Chapter 5 IoT Design methodologiesChapter 5 IoT Design methodologies
Chapter 5 IoT Design methodologies
pavan penugonda
 
Coupling and cohesion
Coupling and cohesionCoupling and cohesion
Coupling and cohesion
Sutha31
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
RTS Tech
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Bayesian networks in AI
Bayesian networks in AIBayesian networks in AI
Bayesian networks in AI
Byoung-Hee Kim
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ahmed Salama
 
Er model ppt
Er model pptEr model ppt
Er model ppt
Pihu Goel
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
Ganga Ram
 
Raspberry Pi Using Python
Raspberry Pi Using PythonRaspberry Pi Using Python
Raspberry Pi Using Python
Seggy Segaran
 
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
IOT DATA MANAGEMENT AND COMPUTE STACK.pptxIOT DATA MANAGEMENT AND COMPUTE STACK.pptx
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
MeghaShree665225
 
House Price Prediction An AI Approach.
House Price Prediction An AI Approach.House Price Prediction An AI Approach.
House Price Prediction An AI Approach.
Nahian Ahmed
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
raj upadhyay
 
Explainable AI
Explainable AIExplainable AI
Explainable AI
Dinesh V
 

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)

Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
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.
  翻译: