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
Threads in JAVA
Haldia Institute of Technology
 
PDF
Java thread life cycle
Archana Gopinath
 
PPT
Structure of a C program
David Livingston J
 
PPTX
Java package
CS_GDRCST
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Breadth First Search & Depth First Search
Kevin Jadiya
 
PPTX
Expert system
Sayeed Far Ooqui
 
PPT
finding Min and max element from given array using divide & conquer
Swati Kulkarni Jaipurkar
 
PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PPTX
8 queen problem
NagajothiN1
 
PPTX
I/O Streams
Ravi Chythanya
 
PPT
RECURSION IN C
v_jk
 
PPTX
Types of Parser
SomnathMore3
 
PDF
Python Dictionary
Soba Arjun
 
PPTX
Compiler vs interpreter
Paras Patel
 
PPT
Method overriding
Azaz Maverick
 
PPT
Heuristc Search Techniques
Jismy .K.Jose
 
PPT
Algorithm analysis
sumitbardhan
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
Java thread life cycle
Archana Gopinath
 
Structure of a C program
David Livingston J
 
Java package
CS_GDRCST
 
JAVA AWT
shanmuga rajan
 
Breadth First Search & Depth First Search
Kevin Jadiya
 
Expert system
Sayeed Far Ooqui
 
finding Min and max element from given array using divide & conquer
Swati Kulkarni Jaipurkar
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
8 queen problem
NagajothiN1
 
I/O Streams
Ravi Chythanya
 
RECURSION IN C
v_jk
 
Types of Parser
SomnathMore3
 
Python Dictionary
Soba Arjun
 
Compiler vs interpreter
Paras Patel
 
Method overriding
Azaz Maverick
 
Heuristc Search Techniques
Jismy .K.Jose
 
Algorithm analysis
sumitbardhan
 
this keyword in Java.pptx
ParvizMirzayev2
 

Similar to Java gui event (20)

PPTX
Event handling
swapnac12
 
PDF
Event Handling in Java as per university
Sanjay Kumar
 
PPT
Chapter 8 event Handling.ppt m m m m m m
zmulani8
 
PPTX
Event Handling in JAVA
Srajan Shukla
 
PPT
Unit 6 Java
arnold 7490
 
PDF
java-programming GUI- Event Handling.pdf
doraeshin04
 
PPTX
What is Event
Asmita Prasad
 
PPTX
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPT
unit-6.pptbjjdjdkd ndmdkjdjdjjdkfjjfjfjfj
sangeethajadhav9901
 
PPTX
tL20 event handling
teach4uin
 
PDF
Ajp notes-chapter-03
Ankit Dubey
 
PPTX
Advance java programming- Event handling
vidyamali4
 
PPTX
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
PPT
Events1
Nuha Noor
 
PPTX
event-handling.pptx
usvirat1805
 
PPTX
Event Handling in Java
Ayesha Kanwal
 
PPTX
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
KGowtham16
 
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Event handling
swapnac12
 
Event Handling in Java as per university
Sanjay Kumar
 
Chapter 8 event Handling.ppt m m m m m m
zmulani8
 
Event Handling in JAVA
Srajan Shukla
 
Unit 6 Java
arnold 7490
 
java-programming GUI- Event Handling.pdf
doraeshin04
 
What is Event
Asmita Prasad
 
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
unit-6.pptbjjdjdkd ndmdkjdjdjjdkfjjfjfjfj
sangeethajadhav9901
 
tL20 event handling
teach4uin
 
Ajp notes-chapter-03
Ankit Dubey
 
Advance java programming- Event handling
vidyamali4
 
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Events1
Nuha Noor
 
event-handling.pptx
usvirat1805
 
Event Handling in Java
Ayesha Kanwal
 
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
KGowtham16
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Ad

Recently uploaded (20)

PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Digital Circuits, important subject in CS
contactparinay1
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
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.