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();
}
}

More Related Content

What's hot (20)

PPTX
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
PPTX
Dynamic Programming
Sahil Kumar
 
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
PPTX
Means End Analysis (MEA) in Artificial.pptx
suchita74
 
PPT
Oops ppt
abhayjuneja
 
PPTX
Binary search in data structure
Meherul1234
 
PPT
Introduction to data structures and Algorithm
Dhaval Kaneria
 
PDF
An introduction to MQTT
Alexandre Moreno
 
PPTX
BNF & EBNF
AshaniDickowita
 
PPT
ADO .Net
DrSonali Vyas
 
PPT
Awt controls ppt
soumyaharitha
 
PDF
Vector
Joyjit Choudhury
 
PPTX
search strategies in artificial intelligence
Hanif Ullah (Gold Medalist)
 
PPTX
Artificial Intelligence
Jay Nagar
 
PPTX
Lecture 1- Artificial Intelligence - Introduction
Student at University Of Malakand, Pakistan
 
PPTX
Genetic algorithms in Data Mining
Atul Khanna
 
PPTX
message communication protocols in IoT
FabMinds
 
PPTX
Ado.Net Tutorial
prabhu rajendran
 
PPT
Introduction to Algorithms
Venkatesh Iyer
 
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
 
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
Dynamic Programming
Sahil Kumar
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Means End Analysis (MEA) in Artificial.pptx
suchita74
 
Oops ppt
abhayjuneja
 
Binary search in data structure
Meherul1234
 
Introduction to data structures and Algorithm
Dhaval Kaneria
 
An introduction to MQTT
Alexandre Moreno
 
BNF & EBNF
AshaniDickowita
 
ADO .Net
DrSonali Vyas
 
Awt controls ppt
soumyaharitha
 
search strategies in artificial intelligence
Hanif Ullah (Gold Medalist)
 
Artificial Intelligence
Jay Nagar
 
Lecture 1- Artificial Intelligence - Introduction
Student at University Of Malakand, Pakistan
 
Genetic algorithms in Data Mining
Atul Khanna
 
message communication protocols in IoT
FabMinds
 
Ado.Net Tutorial
prabhu rajendran
 
Introduction to Algorithms
Venkatesh Iyer
 
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
 

Similar to Java gui event (20)

PPT
Unit 6 Java
arnold 7490
 
PPTX
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
PDF
Unit-3 event handling
Amol Gaikwad
 
PPTX
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
KGowtham16
 
PPTX
Event Handling in JAVA
Srajan Shukla
 
PPTX
Advance java programming- Event handling
vidyamali4
 
PPTX
Event Handling in Java
Ayesha Kanwal
 
PPTX
Event handling
swapnac12
 
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
PDF
Ajp notes-chapter-03
Ankit Dubey
 
PPTX
What is Event
Asmita Prasad
 
PPTX
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
PPTX
ITE 1122_ Event Handling.pptx
udithaisur
 
PPT
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindiappsdevelopment
 
PDF
Java Programming :Event Handling(Types of Events)
simmis5
 
DOCX
Dr Jammi Ashok - Introduction to Java Material (OOPs)
jammiashok123
 
PPT
Graphical User Interface (GUI) - 2
PRN USM
 
PPTX
event-handling.pptx
Good657694
 
Unit 6 Java
arnold 7490
 
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Unit-3 event handling
Amol Gaikwad
 
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
KGowtham16
 
Event Handling in JAVA
Srajan Shukla
 
Advance java programming- Event handling
vidyamali4
 
Event Handling in Java
Ayesha Kanwal
 
Event handling
swapnac12
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
Ajp notes-chapter-03
Ankit Dubey
 
What is Event
Asmita Prasad
 
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
ITE 1122_ Event Handling.pptx
udithaisur
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindiappsdevelopment
 
Java Programming :Event Handling(Types of Events)
simmis5
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
jammiashok123
 
Graphical User Interface (GUI) - 2
PRN USM
 
event-handling.pptx
Good657694
 
Ad

Recently uploaded (20)

PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
July Patch Tuesday
Ivanti
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
July Patch Tuesday
Ivanti
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
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.