Event Handling

Applets are event-driven programs that use a graphical user interface to interact with the user Events are supported by a number of packages, including java.util, java.awt, and java.awt.event

The Delegation Event Model

  • The modern approach to handling events is based on the delegation event model
  • Defines standard and consistent mechanisms to generate and process events
  • A source generates an event and sends it to one or more listeners
  • The listener simply waits until it receives an event
  • Once an event is received, the listener processes the event and then returns
  • In the delegation event model, listeners must register with a source in order to receive an event notification

 

Events

  • An event is an object that describes a state change in a source.
  • It can be generated as a consequence of a person interacting with the elements in a graphical user interface
  • Some of the activities that cause events to be generated are
    •  pressing a button,
    • entering a character via the keyboard,
    • selecting an item in a list, and clicking the mouse.
    • Many other user operations

 

Event Sources

  • A source is an object that generates an event.
  •  This occurs when the internal state of that object changes in some way.
  • Sources may generate more than one type of 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.
  • general form:  public void addTypeListener(TypeListener el)

Type is the name of the event and el is a reference to the event listener

  • Example: the method that registers a keyboard event listener is addKeyListener( )

 

Event Listeners

·         A listener is an object that is notified when an event occurs.

·         It has two major requirements.

o   it must have been registered with one or more sources to receive notifications

o   it must implement methods to receive and process these notifications.

 

Event Classes

·         The most widely used events are those defined by the AWT and those defined by Swing.

·         At the root of the Java event class hierarchy is EventObject, which is in java.util. It is the superclass for all events.

 

Main Event Classes in java.awt.event











The MouseEvent Class






Sources of Events









Event Listener Interfaces

·         Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package

·         When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.

commonly used listener interfaces













The ActionListener Interface

·         This interface defines the actionPerformed( ) method that is invoked when an action event occurs. Its general form is shown  here:

void actionPerformed(ActionEvent ae)

·         The KeyListener Interface

o   void keyPressed(KeyEvent ke)

o   void keyReleased(KeyEvent ke)

o   void keyTyped(KeyEvent ke)

·         The MouseListener Interface

o   void mouseClicked(MouseEvent me)

o   void mouseEntered(MouseEvent me)

o   void mouseExited(MouseEvent me)

o   void mousePressed(MouseEvent me)

o   void mouseReleased(MouseEvent me)

Handling Mouse Events program

import java.awt.*;

 import java.awt.event.*;

 import java.applet.*;

 /*

<applet code="MouseEvents" width=300 height=100>

</applet>

*/

public class MouseEvents extends Applet implements MouseListener, MouseMotionListener {

String msg = "";

int mouseX = 0, mouseY = 0; // coordinates of mouse

 public void init() {

 addMouseListener(this);

addMouseMotionListener(this);

 }

 // Handle mouse clicked.

public void mouseClicked(MouseEvent me) {

// save coordinates

 mouseX = 0;

 mouseY = 10;

msg = "Mouse clicked.";

 repaint();

 }

// Handle mouse entered.

public void mouseEntered(MouseEvent me) {

// save coordinates

mouseX = 0;

 mouseY = 10;

msg = "Mouse entered.";

 repaint();

}

// Handle mouse exited.

public void mouseExited(MouseEvent me) {

 // save coordinates

mouseX = 0;

 mouseY = 10;

msg = "Mouse exited.";

 repaint();

 }

 // Handle button pressed.

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

 }

// Handle mouse dragged.

public void mouseDragged(MouseEvent me) {

 // save coordinates

mouseX = me.getX();

 mouseY = me.getY();

msg = "*";

showStatus("Dragging mouse at " + mouseX + ", " + mouseY);

repaint();

}

// Handle mouse moved.

public void mouseMoved(MouseEvent me)

 {

// show status

showStatus("Moving mouse at " + me.getX() + ", " + me.getY());

}

 // Display msg in applet window at current X,Y location.

public void paint(Graphics g)

 {

 g.drawString(msg, mouseX, mouseY);

}

 }

 

 


 

 

No comments:

Post a Comment