What is an Event Handling and describe the components in Event Handling in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

The GUI in Java processes the interactions with users via mouse, keyboard and various user controls such as button, checkbox, text field, etc. as the events. These events are to be handled properly to implement Java as an Event-Driven Programming.

Components in Event Handling

  • Events
  • Event Sources
  • Event Listeners/Handlers


  • The events are defined as an object that describes a change in the state of a source object.
  • The Java defines a number of such Event Classes inside java.awt.event package
  • Some of the events are ActionEvent, MouseEvent, KeyEvent, FocusEvent,  ItemEvent and etc.

Event Sources

  • A source is an object that generates an event.
  • An event generation occurs when an internal state of that object changes in some way.
  • A source must register listeners in order for the listeners to receive the notifications about a specific type of event.
  • Some of the event sources are Button, CheckBox, List, Choice, Window and etc.

Event Listeners

  • A listener is an object that is notified when an event occurs.
  • A Listener has two major requirements, it should be registered to one more source object to receiving event notification and it must implement methods to receive and process those notifications.
  • Java has defined a set of interfaces for receiving and processing the events under the java.awt.event package.
  • Some of the listeners are ActionListener, MouseListener, ItemListener, KeyListener, WindowListener and etc.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventListenerTest extends JFrame implements ActionListener {
   JButton button;
   public static void main(String args[]) {
      EventListenerTest object = new EventListenerTest();
   void createGUI() {
      button = new JButton(" Click Me !");
   public void actionPerformed(ActionEvent ae) {
      if(ae.getSource() == button) {
         JOptionPane.showMessageDialog(null, "Generates an Action Event");


Updated on 07-Feb-2020 06:34:45