Computer Science Notes Chapter 14 Page 5 of 5
Chapter 15: Event_Driven Programming
These notes are meant to accompany Introduction to Java Programming: Brief Version, seventh edition by Y. Daniel Liang.
Big Skills:
- jjj
Book’s Statement of Skills:
- To describe events, event sources, and event classes.
- To declare listener classes, register listener objects with the source object, and write the code to handle events.
- To declare listener classes using inner classes.
- To declare listener classes using anonymous inner classes.
- To write programs to deal with ActionEvent.
- To write programs to deal with WindowEvent.
- To simplify code for listener classes using listener interface adapters.
- To write programs to deal with MouseEvent.
- To write programs to deal with KeyEvent.
- To use the javax.swing.Timer class to deal with animations.
Section 15.1: Introduction
· Event-driven programming involves writing code that is executed when an event occurs (like a button click), and not in procedural order.
Section 15.2: Event and Event Source
· An event is a signal to a program that something has happened.
o Like mouse movements, button clicks, keystrokes, or internal program activities like a timer
· The source object (or source component) is the component on which the event is fired or generated.
o The root class of all event classes is: java.util.EventObject.
o See image on next page for subclasses of EventObject.
o The getSource() method of the EventObject class identifies the source object of an event.
o If a component can fire an event, then any subclass of the component can, also.
Section 15.3: Listeners, Registrations, and Handling Events
· A source object fires an event, and a listener object handles the event.
· To be a listener, the following two things are needed:
o The listener must be an instance of the corresponding event-listener interface. This ensures it has a method to handle the event. Java has an interface for every type of GUI event (pattern: ActionListener is the event interface for the ActionEvent event…). Methods prescribed in the interface to process an event are called handlers.
o The listener object must be registered by the source object (pattern: addActionListener() is the name of the method to register an ActionEvent event…).
· Here is an old example from Chapter 11: HandleEvent.java
o The following line creates a source object:
JButton jbtOK = new JButton("OK");
o The following line creates a listener object:
OKListenerClass listener1 = new OKListenerClass();
o The following line registers a listener:
jbtOK.addActionListener(listener1);
import java.awt.event.*;
/**
* The HandleEvent class is a GUI class that displays two buttons.
*/
public class HandleEvent extends JFrame
{
/**
* Construct a HandleEvent object with two buttons.
*/
public HandleEvent()
{
// Create two buttons
JButton jbtOK = new JButton("OK");
JButton jbtCancel = new JButton("Cancel");
// Create a panel to hold buttons
JPanel panel = new JPanel();
panel.add(jbtOK);
panel.add(jbtCancel);
add(panel); // Add panel to the frame
// Register listeners
OKListenerClass listener1 = new OKListenerClass();
CancelListenerClass listener2 = new CancelListenerClass();
jbtOK.addActionListener(listener1);
jbtCancel.addActionListener(listener2);
}//end constructor HandleEvent()
/**
* Creates an instance of the HandleEvent class and displays the GUI.
* @param args is not used
*/
public static void main(String[] args)
{
JFrame frame = new HandleEvent();
frame.setTitle("Handle Event");
frame.setSize(200, 150);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//end method main(String[])
}//end class HandleEvent
/**
* The OKListenerClass is a listener for the "OK" button of the HandleEvent class.
* The OKListenerClass implements the ActionListener interface for the "OK" button
* of the HandleEvent class.
*/
class OKListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("OK button clicked");
}//end method actionPerformed(ActionEvent)
}//end inner class CancelListenerClass
/**
* The CancelListenerClass is a listener for the "Cancel" button of the HandleEvent class.
* The CancelListenerClass implements the ActionListener interface for the "Cancel" button
* of the HandleEvent class.
*/
class CancelListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Cancel button clicked");
}//end method actionPerformed(ActionEvent)
}//end inner class CancelListenerClass
· See www.cs.armstrong.edu/liang/intro7e/book/SimpleEventDemo.java
Section 15.3.1: Inner Class Listeners
· An inner class (or nested class) is a class defined within the scope of another class. Inner classes are frequently used to define listener objects in GUI classes, because those listener objects are created specifically for the GUI component.
· See www.cs.armstrong.edu/liang/intro7e/book/SimpleEventDemoInnerClass.java
Section 15.3.2: Anonymous Class Listeners
· An anonymous inner class is an inner class without a name that is defined “on the spot” when registering a listener.
· Syntax:
new SuperClassName / InterfaceName() {//methods…}}
· See www.cs.armstrong.edu/liang/intro7e/book/AnonymousListenerDemo.java
Section 15.3.3: Example: Enlarging or Shrinking a Circle
· See www.cs.armstrong.edu/liang/intro7e/book/ControlBall.java
Section 15.3.4: Handling Window Events
· See www.cs.armstrong.edu/liang/intro7e/book/TestWindowEvent.java
Section 15.3.5: Listener Interface Adapters
· A convenience adapter is a support class provided in Java with default implementations (i.e., empty bodies) for all of the methods in every listener interface, so you do not have to implement all the methods yourself.
· See www.cs.armstrong.edu/liang/intro7e/book/AdapterDemo.java
Section 15.4: Mouse Events
· A mouse event is fired whenever a mouse is pressed, released, clicked, moved, or dragged on a component.
o MouseListener and MouseMotionListener are the listener interfaces for mouse events.
Section 15.4.1: example: Moving a Message on a Panel Using a Mouse
· See www.cs.armstrong.edu/liang/intro7e/book/MoveMessageDemo.java
Section 15.5: Key Events
· A key event is fired whenever a key on the keyboard is pressed, released, or typed on a component.
o The KeyEvent class describes the nature of the event.
o The KeyListener interface is the listener that handles key events.
· See www.cs.armstrong.edu/liang/intro7e/book/KeyEventDemo.java
Section 15.6: Animation Using the Timer Class
· The javax.swing.Timer class is a source component that fires an ActionEvent at a predefined rate.
· See www.cs.armstrong.edu/liang/intro7e/book/AnimationDemo.java
· See www.cs.armstrong.edu/liang/intro7e/book/ClockAnimation.java