Home | | Object Oriented Programming | | The Complete Reference Java | | Internet Programming | | Web Programming | Using the Delegation Event Model - Java

Chapter: Java The Complete Reference - The Java Library - Event Handling

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Using the Delegation Event Model - Java

Now that you have learned the theory behind the delegation event model and have had an overview of its various components, it is time to see it in practice. Using the delegation event model is actually quite easy.

Using the Delegation Event Model


Now that you have learned the theory behind the delegation event model and have had an overview of its various components, it is time to see it in practice. Using the delegation event model is actually quite easy. Just follow these two steps:

 

            Implement the appropriate interface in the listener so that it can receive the type of event desired.

 

            Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications.

 

Remember that a source may generate several types of events. Each event must be registered separately. Also, an object may register to receive several types of events, but it must implement all of the interfaces that are required to receive these events.

To see how the delegation model works in practice, we will look at examples that handle two commonly used event generators: the mouse and keyboard.

 

Handling Mouse Events

 

To handle mouse events, you must implement the MouseListener and the MouseMotionListener interfaces. (You may also want to implement MouseWheelListener, but we won’t be doing so, here.) The following applet demonstrates the process. It displays the current coordinates of the mouse in the applet’s status window. Each time a button is pressed, the word "Down" is displayed at the location of the mouse pointer. Each time the button is released, the word "Up" is shown. If a button is clicked, the message "Mouse clicked" is displayed in the upper-left corner of the applet display area.

As the mouse enters or exits the applet window, a message is displayed in the upper-left corner of the applet display area. When dragging the mouse, a * is shown, which tracks with the mouse pointer as it is dragged. Notice that the two variables, mouseX and mouseY, store the location of the mouse when a mouse pressed, released, or dragged event occurs. These coordinates are then used by paint( ) to display output at the point of these occurrences.

 

// Demonstrate the mouse event handlers.

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

 

}

 

}

Sample output from this program is shown here:


Let's look closely at this example. The MouseEvents class extends Applet and implements both the MouseListener and MouseMotionListener interfaces. These two interfaces contain methods that receive and process the various types of mouse events. Notice that the applet

is both the source and the listener for these events. This works because Component, which supplies the addMouseListener( ) and addMouseMotionListener( ) methods, is a superclass of Applet. Being both the source and the listener for events is a common situation for applets.

Inside init( ), the applet registers itself as a listener for mouse events. This is done by using addMouseListener( ) and addMouseMotionListener( ), which, as mentioned, are members of Component. They are shown here:

 

void addMouseListener(MouseListener ml)

 

void addMouseMotionListener(MouseMotionListener mml)

 

Here, ml is a reference to the object receiving mouse events, and mml is a reference to the object receiving mouse motion events. In this program, the same object is used for both.

 

The applet then implements all of the methods defined by the MouseListener and MouseMotionListener interfaces. These are the event handlers for the various mouse events. Each method handles its event and then returns.

 

Handling Keyboard Events

 

To handle keyboard events, you use the same general architecture as that shown in the mouse event example in the preceding section. The difference, of course, is that you will be implementing the KeyListener interface.

Before looking at an example, it is useful to review how key events are generated. When a key is pressed, a KEY_PRESSED event is generated. This results in a call to the keyPressed( ) event handler. When the key is released, a KEY_RELEASED event is generated and the keyReleased( ) handler is executed. If a character is generated by the keystroke, then a KEY_TYPED event is sent and the keyTyped( ) handler is invoked. Thus, each time the user presses a key, at least two and often three events are generated. If all you care about are actual characters, then you can ignore the information passed by the key press and release events. However, if your program needs to handle special keys, such as the arrow or function keys, then it must watch for them through the keyPressed( ) handler.

The following program demonstrates keyboard input. It echoes keystrokes to the applet window and shows the pressed/released status of each key in the status window.

// Demonstrate the key event handlers.

import java.awt.*;

import java.awt.event.*;

import java.applet.*; /*

 

<applet code="SimpleKey" width=300 height=100> </applet>

 

*/

 

public class SimpleKey extends Applet implements KeyListener {

 

String msg = "";

 

int X = 10, Y = 20; // output coordinates

 

public void init() { addKeyListener(this);

 

}

 

public void keyPressed(KeyEvent ke) { showStatus("Key Down");

 

}

 

public void keyReleased(KeyEvent ke) { showStatus("Key Up");

 

}

 

public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint();

 

}

 

// Display keystrokes.

 

public void paint(Graphics g) { g.drawString(msg, X, Y);

 

}

 

}

 

Sample output is shown here:


If you want to handle the special keys, such as the arrow or function keys, you need to respond to them within the keyPressed( ) handler. They are not available through keyTyped( ).

To identify the keys, you use their virtual key codes. For example, the next applet outputs the name of a few of the special keys:

 

// Demonstrate some virtual key codes.

import java.awt.*;

 

import java.awt.event.*; import java.applet.*; /*

 

<applet code="KeyEvents" width=300 height=100> </applet>

 

*/

 

public class KeyEvents extends Applet implements KeyListener {

 

String msg = "";

 

int X = 10, Y = 20; // output coordinates

 

public void init() { addKeyListener(this);

 

}

 

public void keyPressed(KeyEvent ke) { showStatus("Key Down");

 

int key = ke.getKeyCode(); switch(key) {

 

case KeyEvent.VK_F1: msg += "<F1>"; break;

 

case KeyEvent.VK_F2: msg += "<F2>"; break;

 

case KeyEvent.VK_F3: msg += "<F3>"; break;

 

case KeyEvent.VK_PAGE_DOWN: msg += "<PgDn>";

break;

 

case KeyEvent.VK_PAGE_UP: msg += "<PgUp>";

break;

 

case KeyEvent.VK_LEFT: msg += "<Left Arrow>"; break;

 

case KeyEvent.VK_RIGHT: msg += "<Right Arrow>"; break;

 

}

 

repaint();

 

}

public void keyReleased(KeyEvent ke) { showStatus("Key Up");

 

}

 

public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint();

 

}

 

// Display keystrokes.

 

public void paint(Graphics g) { g.drawString(msg, X, Y);

 

}

 

}

 

Sample output is shown here:


The procedures shown in the preceding keyboard and mouse event examples can be generalized to any type of event handling, including those events generated by controls. In later chapters, you will see many examples that handle other types of events, but they will all follow the same basic structure as the programs just described.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.