Вы находитесь на странице: 1из 44

8 GUI Event Handling

Introduction to Programming 2

Topics

The Delegation Event Model Event Classes Event Listeners


ActionListener Method MouseListener Methods MouseMotionListener Methods WindowListener Methods Guidelines for Creating Applications Handling GUI Events
Introduction to Programming 2 2

Topics

Adapter Classes Inner Classes Anonymous Inner Classes

Introduction to Programming 2

The Delegation Event Model

The Delegation Event Model


Model used by Java to handle user interaction with GUI components Describes how your program can respond to user interaction

Three important components:


Event Source Event Listener/Handler Event Object

Introduction to Programming 2

The Delegation Event Model

Event Source

GUI component that generates the event Example: button, mouse, keyboard

Event Listener/Handler

Receives news of events and processes user's interaction Example: displaying an information useful to the user, computing for a value

Introduction to Programming 2

The Delegation Event Model

Event Object

Created when an event occurs (i.e., user interacts with a GUI component) Contains all necessary information about the event that has occurred

Type of event that has occurred Source of the event

May have one of several event classes as data type

Introduction to Programming 2

The Delegation Event Model

A listener should be registered with a source Once registered, listener waits until an event occurs When an event occurs

An event object created Event object is fired by the source to the registered listeners

Once the listener receives an event object from the source


Deciphers the notification Processes the event that occurred.


Introduction to Programming 2 7

The Delegation Event Model

Introduction to Programming 2

Registration of Listeners

Event source registering a listener:


void add<Type>Listener(<Type>Listener listenerObj) where,

<Type> depends on the type of event source

Can be Key, Mouse, Focus, Component, Action and others

One event source can register several listeners

Registered listener being unregistered:


void remove<Type>Listener(<Type>Listener listenerObj)

Introduction to Programming 2

Event Classes

An event object has an event class as its reference data type The EventObject class

Found in the java.util package

The AWTEvent class


An immediate subclass of EventObject Defined in java.awt package Root of all AWT-based events Subclasses follow this naming convention: <Type>Event
Introduction to Programming 2 10

Event Classes

Introduction to Programming 2

11

Event Listeners

Classes that implement the <Type>Listener interfaces Common <Type>Listener interfaces:

Introduction to Programming 2

12

ActionListener Method

Contains exactly one method

Introduction to Programming 2

13

MouseListener Methods

Introduction to Programming 2

14

MouseMotionListener Methods

Introduction to Programming 2

15

WindowListener Methods

Introduction to Programming 2

16

Creating GUI Applications with Event Handling

Guidelines:
1. Create a GUI class

Describes and displays the appearance of your GUI application

2. Create a class implementing the appropriate listener interface

May refer to the same class as step 1

3. In the implementing class

Override ALL methods of the appropriate listener interface Describe in each method how you would like the event to be handled May give empty implementations for methods you don't need

4. Register the listener object with the source

The object is an instantiation of the listener class in step 2 Use the add<Type>Listener method

Introduction to Programming 2

17

Mouse Events Example


1 2 3

import java.awt.*; import java.awt.event.*; public class MouseEventsDemo extends Frame implements MouseListener, MouseMotionListener { TextField tf; public MouseEventsDemo(String title){ super(title); tf = new TextField(60); addMouseListener(this); } //continued...
Introduction to Programming 2 18

4 5 6 7 8 9 10

Mouse Events Example


11 12 13 14 15 16 17 18 19 20 21

public void launchFrame() { /* Add components to the frame */ add(tf, BorderLayout.SOUTH); setSize(300,300); setVisible(true); } public void mouseClicked(MouseEvent me) { String msg = "Mouse clicked."; tf.setText(msg); } //continued...
Introduction to Programming 2 19

Mouse Events Example


22 23 24 25 26 27 28 29 30 31 32 33 34

public void mouseEntered(MouseEvent me) { String msg = "Mouse entered component."; tf.setText(msg); } public void mouseExited(MouseEvent me) { String msg = "Mouse exited component."; tf.setText(msg); } public void mousePressed(MouseEvent me) { String msg = "Mouse pressed."; tf.setText(msg); } //continued...
Introduction to Programming 2 20

Mouse Events Example


35 36 37 38 39 40 41 42 43 44

public void mouseReleased(MouseEvent me) { String msg = "Mouse released."; tf.setText(msg); } public void mouseDragged(MouseEvent me) { String msg = "Mouse dragged at " + me.getX() + "," + me.getY(); tf.setText(msg); } //continued...

Introduction to Programming 2

21

Mouse Events Example


45 46 47 48 49 50 51 52 53 54 55

public void mouseMoved(MouseEvent me) { String msg = "Mouse moved at " + me.getX() + "," + me.getY(); tf.setText(msg); } public static void main(String args[]) { MouseEventsDemo med = new MouseEventsDemo("Mouse Events Demo"); med.launchFrame(); } }
Introduction to Programming 2 22

Mouse Events Example

Running MouseEventsDemo:

Introduction to Programming 2

23

Mouse Events Example

Running MouseEventsDemo (mouse enters frame):

Introduction to Programming 2

24

Mouse Events Example

Running MouseEventsDemo (mouse exits frame):

Introduction to Programming 2

25

Close Window Example


1 2 3 4 5 6 7 8 9 10 11 12

import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame implements WindowListener { Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener(this); } //continued...
Introduction to Programming 2 26

Close Window Example


13 14 15 16 17 18 19 20 21 22 23 24 25

void launchFrame() { setSize(300,300); setVisible(true); } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } //continued...
Introduction to Programming 2 27

Close Window Example


26 27 28 29 30 31 32 33 34 35 36 37 38 39

public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); cf.launchFrame(); } }
Introduction to Programming 2 28

Close Window Example

Introduction to Programming 2

29

Adapter Classes

Why use Adapter classes?


Implementing all methods of an interface takes a lot of work Interested in implementing some methods of the interface only

Adapter classes

Built-in in Java Implement all methods of each listener interface with more than one method Implementations of the methods are all empty

Introduction to Programming 2

30

Adapter Classes: Close Window Example


1 2 3 4 5 6 7 8 9 10 11 12 13

import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame{ Label label; CFListener w = new CFListener(this); CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener(w); } //continued...
Introduction to Programming 2 31

Adapter Classes: Close Window Example


14 15 16 17 18 19 20 21 22 23 24 25

void launchFrame() { setSize(300,300); setVisible(true); } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); cf.launchFrame(); } } //continued...
Introduction to Programming 2 32

Adapter Classes: Close Window Example


25 26 27 28 29 30 31 32 33 34 35

class CFListener extends WindowAdapter { CloseFrame ref; CFListener( CloseFrame ref ){ this.ref = ref; } public void windowClosing(WindowEvent e) { ref.dispose(); System.exit(1); } }
Introduction to Programming 2 33

Inner Classes

Class declared within another class Why use inner classes?


Help simplify your programs Especially in event handling

Introduction to Programming 2

34

Inner Classes: Close Window Example


1 2 3 4 5 6 7 8 9 10 11 12

import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame{ Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener(new CFListener()); } //continued...
Introduction to Programming 2 35

Inner Classes: Close Window Example


13 14 15 16 17 18 19 20 21 22 23 24

void launchFrame() { setSize(300,300); setVisible(true); } class CFListener extends WindowAdapter { public void windowClosing(WindowEvent e) { dispose(); System.exit(1); } } //continued...
Introduction to Programming 2 36

Inner Classes: Close Window Example


25 26 27 28 29 30

public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window cf.launchFrame(); } } Example");

Introduction to Programming 2

37

Anonymous Inner Classes

Unnamed inner classes Why use anonymous inner classes?


Further simplify your codes Especially in event handling

Introduction to Programming 2

38

Anonymous Inner Classes: Close Window Example


1 2 3 4 5 6 7 8 9 10 11 12 13

import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame{ Label label; CloseFrame(String title) { super(title); label = new Label("Close the frame."); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(1); } }); }
Introduction to Programming 2 39

Anonymous Inner Classes: Close Window Example


14 15 16 17 18 19 20 21 22 23 24

void launchFrame() { setSize(300,300); setVisible(true); } public static void main(String args[]) { CloseFrame cf = new CloseFrame("Close Window Example"); cf.launchFrame(); } }
Introduction to Programming 2 40

Summary

The Delegation Event Model

Register listeners void add<Type>Listener(<Type>Listener listenerObj) Listeners wait for an event to occur When event occurs:

Event object created Object is fired by source to registered listeners

When listener receives event object:

Deciphers notification Processes the event

Introduction to Programming 2

41

Summary

The Delegation Event Model Components


Event Source Event Listener/Handler Event Object

Event Classes

The EventObject Class The AWTEvent Class

Root of all AWT-based events Subclasses follow this naming convention:

<Type>Event
Introduction to Programming 2 42

Summary

Event Listeners

ActionListener Method MouseListener Methods MouseMotionListener Methods WindowListener Methods

Introduction to Programming 2

43

Summary

Creating GUI Applications with Event Handling


1. Create a GUI class 2. Create a class implementing the appropriate listener interface 3. In the implementing class

Override ALL methods of the appropriate listener interface Describe in each method how you would like the event to be handled

4. Register the listener object with the source

Use the add<Type>Listener method

Simplifying your code:


Adapter Classes Inner Classes Anonymous Inner Classes


Introduction to Programming 2 44