Handling Events In Java

Introduction

 
GUI provides an interface through which a user interacts with the application. It is necessary that the application responds to the actions performed by the user.
 
The user actions are represented through events in the Java programming language. To handle these events, Java provides a mechanism event model.
 
An event is handled by defining and associating a handler for the event. Defining a handler enables the application to respond to the event, and associating a handler enables the application to bind the handler with the event.
 
The event model contains three components:
  • Event Source
  • Event listener
  • Event handler

Event Source

 
An event source is an object that generates an event. The object registers some handler, which receives notifications about a specific type of event. There are various components in the Swing toolkit such as button, frame, and combo box, which act as an event source. These sources generate various events, for example, a button generates an action event when the button is clicked, or a frame generates the window event when the window is opened or closed.
 

Event Listener

 
An event listener listens for a specific event and gets notified when the specific events occur. An event listener registers with one or more event sources to receive notifications about specific types of events and to process the events. The information may contain the source that generated the event and the time when the event was generated.
 

Event handler

 
An event handler is a method that is invoked by an event listener when the event is generated. Event listeners are the interfaces and event handlers are the methods declared in the event listener interface that is implemented by the application.
 

Event classes

 
Event classes represent the characteristic of all events, such as source, time, and ID of the event. When an event occurs, the related object of the event is created.
 
The event classes are:
  • Action Event
  • KeyEvent
  • Mouse Event
  • FocusEvent
  • Item Event
  • Window Event
The Event object class, which is in the Java.util package, is the superclass for all the event classes. The class, Event Object, from which all event objects are derived, extends the event objects class and implements the serializable interface.
 
Action Event
 
The action event is generated by components, such as a button when an action is performed.For example, clicking a button is an action event generated by the button.
  1. import java.awt.event.ActionEvent;  
  2. import java.awt.event.ActionListener;  
  3. import javax.swing.JButton;  
  4. import javax.swing.JLabel;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.Jpanel;  
  7. public class ActionEventDemo extends JFrame implements ActionListener {  
  8.     JButton click;  
  9.     JPanel Panle;  
  10.     JLabel message;  
  11.     public ActionEventDemo() {  
  12.         super("Action Event Demo");  
  13.         click = new JButton("Click");  
  14.         panel = new JPanel();  
  15.         message = new JLabel();  
  16.         add(panel);  
  17.         panel.add(click);  
  18.         panel.add(message);  
  19.         setSize(300300);  
  20.         setVisible(true);  
  21.     }  
  22.     public static actionPerformed(ActionEvent e) {  
  23.         message.setText("Welcome to event handling in java");  
  24.     }  
  25.     public static void main(String args[]) {  
  26.         ActionEventDemo obj = new ActionEventDemo();  
  27.     }  
  28. }  
Mouse Event
 
The mouse event is generated by a mouse with respect to the source component. These mouse Events can have two categories,
  • Mouse Events
    It contains the mouse events, such as mouse pressed, mouse clicked, and mouse entered.

  • Mouse motion events
    It contains the mouse motion events, such as mouse moved and mouse dragged. 
KeyEvent
 
The key event is generated by a component when the key is pressed, released, or typed within the component. int getKeycode() is a method in keyevent that returns the integer key code for the key and char getKeyChar() is a method that returns the character associated with the key.
 
FocusEvent
 
The focus event is generated by a component when it receives or loses focus. boolean isTemporary() is a method which identifies whether the focus is changed permanently or temporarily. Component getOppositeComponent() is a method that returns the opposite component that is involved in focus or activation change.
 
ItemEvent
 
The item event is generated by components, such as a radio button, and checkbox, when an item or the component is selected or deselected by the user.
 
Object getItem() is a method that returns the item affected by the event and int getStateChange() is a method that returns the changed state.
 
Window Event
 
The window event is generated by the window when it is opened, closed, activated, deactivated, or when the focus is transferred into or out of the window.
 
int getNewState()-returns the new state of the window.
 
int getOldState() -return the previous state of the window.