Java Event Delegation Model, Listener and Adapter Classes


Java Event Delegation Model


The Delegation Event model is one of the many techniques used to handle events in GUI (Graphical User Interface) programming languages. GUI represents a system where an user visually/graphically interacts with the system. Other interactive systems are text based or called CUI (Character User Interface). CUI interacts with the system by typing out commands in the console. GUI programming is inherently event driven; that means, whenever an user initiates an activity such as a mouse move that changes the co-ordinates of the mouse pointer in the screen, or clicks a button, scrolls a page, and so forth, each is deemed as an event that the GUI event handler maps the activity to a piece of code that explains what response the application should provide to the user.
Any program that uses GUI (graphical user interface) such as Java application written for windows, is event driven. Event describes the change in state of any object. For Example: Pressing a button, Entering a character in Textbox, Clicking or Dragging a mouse, etc.

Components of Event Handling

Event handling has three main components,
·         Events: An event is a change in state of an object.
·         Events Source: Event source is an object that generates an event.
·         Listeners: A listener is an object that listens to the event. A listener gets notified when an event occurs.

 

Events, Sources, and Listeners

The delegation event model can be defined by three components: event, event source, and event listeners.
·         Events: The event object defines the change in state in the event source class. For example, interacting with the graphical interfaces, such as clicking a button or entering text via keyboard in a text box, item selection in a list, all represent some sort of change in the state. The event object is used to carry the required information about the state change. However, all events are not cause by user interaction. There are events such as timer event, hardware/software events, and so forth, that does not depend upon user interaction. They occur automatically. We can define the procedure to handle them once they occur.
·         Event sources: Event sources are objects that cause the events to occur due to some change in the property of the component. Because there can be various types a component can trigger, each must be registered to a listener to provide a suitable response.
·         Event listeners: Event listeners are objects that are notified as soon as a specific event occurs. Event listeners must define the methods to process the notification they are interested to receive.

Figure: Events, sources, and listeners

How Events are handled?

A source generates an Event and sends it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return. Events are supported by a number of Java packages, like java.utiljava.awt and java.awt.event.
   

Important Event Classes and Interface

Event Classes
Description
Listener Interface
ActionEvent
generated when button is pressed, menu-item is selected, list-item is double clicked
ActionListener
MouseEvent
generated when mouse is dragged, moved,clicked,pressed or released and also when it enters or exit a component
MouseListener
KeyEvent
generated when input is received from keyboard
KeyListener
ItemEvent
generated when check-box or list item is clicked
ItemListener
TextEvent
generated when value of textarea or textfield is changed
TextListener
MouseWheelEvent
generated when mouse wheel is moved
MouseWheelListener
WindowEvent
generated when window is activated, deactivated, deiconified, iconified, opened or closed
WindowListener
ComponentEvent
generated when component is hidden, moved, resized or set visible
ComponentEventListener
ContainerEvent
generated when component is added or removed from container
ContainerListener
AdjustmentEvent
generated when scroll bar is manipulated
AdjustmentListener
FocusEvent
generated when component gains or loses keyboard focus
FocusListener

Steps to handle events:

1.     Implement appropriate interface in the class.
2.     Register the component with the listener.

Example of Event Handling


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/*
< applet code="Test" width=300, height=100 >
*/
public class Test extends Applet implements KeyListener
{
 String msg="";
 public void init()
 {
   addKeyListener(this);
 }
 public void keyPressed(KeyEvent k)
 {
   showStatus("KeyPressed");
 }
 public void keyReleased(KeyEvent k)
 {
   showStatus("KeyRealesed");
 }
 public void keyTyped(KeyEvent k)
 {
   msg = msg+k.getKeyChar();
   repaint();
 }
 public void paint(Graphics g)
 {
   g.drawString(msg, 20, 40);
 }
}

Listener Interface

In the Java event hierarchy, we can find that some listener interfaces depend on more than one event handler. Among them, only a few of the event handling methods contain any meaningful code. Most are declared as an empty body. These methods are supposed to be defined by their implementing classes. Suppose we want to implement a mouse events; we either can implement some/all of the mouse events by implementing listener interfaces or extend the adapter class and define only the required events that we want to process.

public class MouseHandler implements MouseListener, MouseMotionListener{

      @Override
      public void mouseDragged(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
      public void mouseMoved(MouseEvent e) {
         // TODO Auto-generated method stub

      }

      @Override
      public void mouseClicked(MouseEvent e) {
         statusLabel.setText(String
         .format("Clicked at (x=%d, y=%d)", e.getX(),
            e.getY()));
      }

      @Override
      public void mousePressed(MouseEvent e) {
         // TODO Auto-generated method stub

      }

      @Override
      public void mouseReleased(MouseEvent e) {
         // TODO Auto-generated method stub

      }

      @Override
      public void mouseEntered(MouseEvent e) {
         statusLabel.setText("Mouse in scope");

      }

      @Override
      public void mouseExited(MouseEvent e) {
         statusLabel.setText("Mouse out of scope");
      }

   }

Adapter Classes

Adapter classes implement the corresponding listener interface. For example, the WindowAdapter class implements WindowListener, which is a listener interface for listening to window events such as closing, activated, opened, and so on, events. Similarly, the MouseAdapter class implements MouseListener. The event methods are all derived from the listener interfaces, but the corresponding adapter classes do not provide any body of the event method. These adapter classes are designated as abstract and are meant to be extended. The class that extends defines one or more of the methods to contain the logic of the event handling process. The adapter classes follow the Adapter pattern. However, not all listeners have their corresponding adapter classes in the Java API.

private class MouseHandler extends MouseAdapter{

      @Override
      public void mouseClicked(MouseEvent e) {
         statusLabel.setText(String
         .format("Clicked at (x=%d, y=%d)", e.getX(),
            e.getY()));

      }

      @Override
      public void mouseEntered(MouseEvent e) {
         statusLabel.setText("Mouse in scope");

      }

      @Override
      public void mouseExited(MouseEvent e) {
         statusLabel.setText("Mouse out of scope");
      }

   }

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1