31
Event Handling An event handler is an asynchronous callback subroutine that handles inputs received in a program. Each event is a piece of application-level informatio n from the underlying framework, typically the GUI toolkit. This allows one component to identify to one or more other components that an event (such as a us er pressing a button) has occurred so that the component can respond to it.

Event Class

Embed Size (px)

Citation preview

Page 1: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 1/31

Event Handling

• An event handler is an asynchronous callback 

subroutine that handles inputs received in a program.

Each event is a piece of application-level information

from the underlying framework, typically the GUItoolkit.

• This allows one component to identify to one or more

other components that an event (such as a user

pressing a button) has occurred so that the component

can respond to it.

Page 2: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 2/31

The Delegation Event Model 

• The modern approach to handling events is

based on the delegation event model 

• Concept:

 – a source generates an event and sends it to one or

more listeners.

 – the listener simply waits until it receives an event.

 – Once received, the listener processes the event

and then returns.

Page 3: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 3/31

Events 

In the delegation model, an event is an object that

describes a state change in a source.

For example, an event may be generated when a

timer expires, a counter exceeds a value, asoftware or hardware failure occurs, or an

operation is completed. We are free to define

events that are appropriate for your application.

Page 4: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 4/31

Event Classes 

• At the root of the Java event class hierarchy is

EventObject, which is in java.util.*

Page 5: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 5/31

Page 6: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 6/31

Event Class

Page 7: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 7/31

Page 8: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 8/31

Event Sources 

• A source is an object that generates an event.

• A source must register listeners in order for

the listeners to receive notifications about a

specific type of event. Each type of event has

its own registration method.

• Here is the general form:

public void addTypeListener(TypeListener el )

Type is the name of the event and

el is a reference to the event listener

Page 9: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 9/31

• Example:

• The method that registers a keyboard event

listener is called addKeyListener( ) 

•   The method that registers a mouse motion

listener is called addMouseMotionListener( )

• Note: When an event occurs, all registeredlisteners are notified and receive a copy of the

event object. This is known as multicasting the

event.

Page 10: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 10/31

Event Source

Page 11: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 11/31

Event Listeners 

It has two major requirements:

• First, it must have been registered with one or

more sources to receive notifications about

specific types of events.

• Second, it must implement methods to

receive and process these notifications.

Page 12: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 12/31

Event Listener Interface

Page 13: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 13/31

The ActionListener Interface 

• void actionPerformed(ActionEvent ae)

Page 14: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 14/31

The AdjustmentListener Interface 

• Void

adjustmentValueChanged(AdjustmentEvent

ae)

Page 15: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 15/31

The ComponentListener Interface 

• void componentResized(ComponentEvent ce)

• void componentMoved(ComponentEvent ce)

void componentShown(ComponentEvent ce)• void componentHidden(ComponentEvent ce)

Page 16: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 16/31

The ContainerListener Interface 

• void componentAdded(ContainerEvent ce)

• void componentRemoved(ContainerEvent ce)

Page 17: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 17/31

The FocusListener Interface 

• void focusGained(FocusEvent fe)

• void focusLost(FocusEvent fe)

Page 18: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 18/31

The ItemListener Interface 

• void itemStateChanged(ItemEvent ie)

Page 19: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 19/31

The KeyListener Interface 

• void keyPressed(KeyEvent ke)

• void keyReleased(KeyEvent ke)

void keyTyped(KeyEvent ke)

Page 20: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 20/31

The MouseListener Interface 

• void mouseClicked(MouseEvent me)

• void mouseEntered(MouseEvent me)

void mouseExited(MouseEvent me)• void mousePressed(MouseEvent me)

• void mouseReleased(MouseEvent me)

Page 21: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 21/31

 The MouseMotionListener

Interface 

• void mouseDragged(MouseEvent me)

• void mouseMoved(MouseEvent me)

Page 22: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 22/31

 The MouseWheelListener

Interface 

• void mouseWheelMoved(MouseWheelEvent

mwe)

Page 23: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 23/31

The TextListener Interface 

• void textChanged(TextEvent te)

Page 24: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 24/31

 The WindowFocusListener

Interface 

• void windowGainedFocus(WindowEvent we)

• void windowLostFocus(WindowEvent we)

Page 25: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 25/31

The WindowListener Interface 

• void windowActivated(WindowEvent we)

• void windowClosed(WindowEvent we)

void windowClosing(WindowEvent we)• void windowDeactivated(WindowEvent we)

• void windowDeiconified(WindowEvent we)

• void windowIconified(WindowEvent we)• void windowOpened(WindowEvent we)

Page 26: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 26/31

Sample Program using Mouse

Event

 

l l d JP l i l M Li

Page 27: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 27/31

• class mousepanel extends JPanel implements MouseListener

• {

• String msg = "";

• int mouseX = 0, mouseY = 0;

• public mousepanel()

• {addMouseListener(this);

• }

• public void mouseClicked(MouseEvent me) {

•  // save coordinates

• mouseX = 50;

• mouseY = 50;

• msg = "Mouse clicked.";

• repaint();

• }

•  // Handle mouse entered.• public void mouseEntered(MouseEvent me) {

•  // save coordinates

• mouseX = 75;

• mouseY = 100;

• msg = "Mouse entered.";

• repaint();

• }

•  // Handle mouse exited.

• public void mouseExited(MouseEvent me) {

•  // save coordinates

• mouseX = 0;

• mouseY = 10;

• msg = "Mouse exited.";

• repaint();

•}

Page 28: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 28/31

• public void mousePressed(MouseEvent me) {

• // save coordinates

• mouseX = me.getX();

• mouseY = me.getY();

•msg = "Down";

• repaint();

• }

• // Handle button released.

• public void mouseReleased(MouseEvent me) {

• // save coordinates

• mouseX = me.getX();• mouseY = me.getY();

• msg = "Up";

• repaint();

• }

• public void paint(Graphics g) {

• g.drawString(msg, mouseX, mouseY);• }

• }

Page 29: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 29/31

• class newframe extends JFrame

• {

• public newframe()

• {

• setTitle("Mouse Event");

• setSize(100,300);

• mousepanel p1=new mousepanel();

• add(p1);

• }

• }

Page 30: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 30/31

• import javax.swing.*;

• import java.awt.*;

•import java.awt.event.*;

• import java.util.*;

• class demo1

• {

• public static void main(String args[])• {

• newframe f1=new newframe();

• f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

• f1.setVisible(true);• }

• }

Page 31: Event Class

8/4/2019 Event Class

http://slidepdf.com/reader/full/event-class 31/31

Sample Output