Java – Event Handling4 min read

Event:

An event is an object that describes the change of state in a source. It is the changes made within the GUI(Graphical User Interface). The activities that cause the event to be generated are the pressing of a button, selecting items, input the information, clicking of the mouse, etc, basically all the interaction with GUI.

Two types of Event:

  • Foreground Events:
    It is the clicking of a mouse, pressing the button, input character from the keyboard. scrolling of the page etc. That is, these are events where the user is to interact directly with the GUI.
  • Background Events:
    The end-user interaction is seen in this type of event such as OS interruption, the timer expires, operation completion, etc. This is not directly caused by interactions with a user interface.

Event Handling:

It is a mechanism that handles all the events occurring and also controls the further process when an event occurs.
To handle the events, Java uses the Delegation Event Model.

Let us learn about the Delegation Event Model.

Delegation Event Model:

This model in Java is a new approach to handle the events. It is the standard and consistent way to Handle the events generated by the interaction with GUI components.

The terms used:

  • Event Source:
    A source is an object that generates a specific kind of event. This may be the GUI component such as button list, Window, etc. Source provides data of the event that is occurred to the handler.
    A listener gets notified about the type of event that occurred and for that source must register a listener.
    General form to register the listener to an event source is:
    publicvoid addTypeListener(TypeEvent e)
    Here, Type is the name of the event and e is the reference of the event listener.
  • Event Listener:
    A listener is an object that gets notified when an event occurs. In order to get notified, it must be registered to one or more event sources and also it must implement methods to be able to receive and process it.
    These methods in Listener is found in, or need to import:
    import java.awt.event.*;
  • Process:
    Once the listener is registered, it waits for the event to occur. And when an event occurs, the source object is created and notified to the listener by the listener methods. Then, when the listener receives that object from the source, it executes the event and returns.

Event Class:

Java provides number of event classes.
Some of them are:

  • ActionEvent:
    It is the easiest and most common event occurring. To write an Action Listener extends a class AWTEvent. Example: when a button is pressed, the item in a list is double-clicked, etc.
  • KeyEvent:
    It extends a class InputEvent. It has three methods in Listener interface.
    public void keyTyped(KeyEvent e)
    {active when typed}
    public void KeyPressed(KeyEvent e)
    {active when pressed}
    public void KeyReleased(KeyEvent e)
    {active when released}
  • MouseEvent:
    It extends a class InputEvent. It has 5 methods.
    It is instantiated when a mouse button is pressed, released, or clicked, or when a mouse cursor enters or exits a visible part of a component.
  • WindowEvent:
    It extends a class InputEvent. It provides 8 methods. Starts from when a Window object is opened, closed, activated, deactivated, iconified, deiconified, or gain or loss of Window Focus.
  • TextEvent:
    It extends a class AWTEvent. This class is instantiated when the text value or text area is changed.
  • ItemEvent:
    It extends AWtEvent. And instantiated when an item or from a list or a checkbox is selected or deselected.

There are more event class such as ComponentEvent, InputEvent, FocusEvent, MouseMotionEvent etc.


EventListener Interfaces:

EventListener Interface class is defined by the java.util.package.
It is an interface where every listener interface has to be extended.

Class Declaration:

The Following shows the declaration for java.util.EventListener interface:

AWT Event Listener Interfaces:

The most commonly used event Listeners are:

  1. ActionListener:
    used for receiving the action events.
  2. AdjustmentListener:
    used for receiving the adjustment events.
  3. ComponentListener:
    used to define when a component is hidden, moved, resized, or shown.
  4. Focus Listener:
    used for receiving the focus events such as when a component loses or gains keyboard focus.
  5. KeyListener:
    used for receiving the key events such as the state of pressed, released, or typed.
  6. TextListener:
    used to receive text events like when the value of text changes.
  7. MouseMotionListener:
    used to receive the mouse motion events like when the mouse is moved or dragged.
  8. ContainerListener:
    used to receive container events like the addition or removal of the component from the container.
  9. WindowListener:
    used to receive window events such as window activation, deactivation, closed, opened or exit.
  10. MouseListener:
    used to receive mouse events such as clicking, pressed or released, etc.
  11. ItemListener:
    used to receive the item events that is the state of change of items.