Event Handling in Java Complete Tutorial

Introduction

Event handling in Java is one of the important topic. It is asked in many java professional interviews and it is very necessary for developers to learn.

When state of an object is changed, it is known as an event in Java.

For example : button click , mouse hover etc.

In Java, there are several event classes present and there are several Listener interfaces available for event handling.

All these event classes and listeners are present in java.awt.event package.

Event Handling in Java

Event Handling is managed by below mentioned aspects :

  1. Event Classes.
  2. Listener Interfaces.
  3. Event Handling Components.
  4. Registration Methods.

Let’s learn each in detail :

Java Event classes and Listener interfaces

Below are the list of event classes and listener interface present in Java :

Event ClassListener Interface
MouseEventMouseListener
MouseMotionListener
KeyEventKeyListener
MouseWheelEventMouseWheelListener
ActionEventActionListener
TextEventTextListener
WindowEventWindowListener
ContainerEventContainerListener
ComponentEventComponentListener
AdjustmentEventAdjustmentListener
FocusEventFocusListener
ItemEventItemListener

Components of Event Handling

There are three components in Event Handling , as mentioned below :

Events

As discussed earlier, Events are simply change in state of object in java.

Events Source

Event Source is that object which generates the event. Some Event Source examples are : button, text field etc.

Listeners

Listener listens to event. It is an object as well. Whenever an event occurs, listeners gets notified.

Process of Event Handling in Java

An event source generates the event. Event source send it to the registered listeners,

As listener receives the event, it process the event and then return it.

Steps to perform Event Handling

Below are the major steps in performing event handling :

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

Registration Methods

We need to register the component with the listener to perform event handling.

This can be done using registration methods provided by many classes :

Class nameMethod
Buttonpublic void addActionListener(ActionListener a){}
TextField1) public void addActionListener(ActionListener a){}
2) public void addTextListener(TextListener a){}
CheckBoxpublic void addItemListener(ItemListener a){}
List1) public void addActionListener(ActionListener a){}
2) public void addItemListener(ItemListener a){}
MenuItempublic void addActionListener(ActionListener a){}
Choicepublic void addItemListener(ItemListener a){}
TextAreapublic void addTextListener(TextListener a){}

Java Event Handling Code

Event Handling code can be placed in below mentioned places :

  1. In class.
  2. In other class.
  3. In anonymous class.

Example : Event handling in Java by implementing ActionListener inside the class

package eventHandling;

import java.awt.*;
import java.awt.event.*;

public class EventApp extends Frame implements ActionListener {

	TextField field;

	EventApp() {

//creating components in the constructor

		field = new TextField();
		field.setBounds(60, 50, 170, 20);
		Button button = new Button("submit");
		button.setBounds(100, 120, 80, 30);

//registering the listener  
//pass the current instance using 'this'

		button.addActionListener(this);

//adding components
//setting size, layout and visibility  

		add(field);
		add(button);
		setSize(300, 300);
		setLayout(null);
		setVisible(true);

	}

	public void actionPerformed(ActionEvent e) {
		field.setText("You clicked the button");
	}

	public static void main(String args[]) {
		new EventApp();
	}

}

Output:

event handling in java
event handling in java

Example : Event handling in Java by implementing ActionListener outside the class

EventApp2.java

package eventHandling;

import java.awt.*;
import java.awt.event.*;

class EventApp2 extends Frame {

	TextField field;

	EventApp2() {

// creating components in the constructor

		field = new TextField();
		field.setBounds(60, 50, 170, 20);
		Button button = new Button("click me");
		button.setBounds(100, 120, 80, 30);

//registering the listener
// pass the current instance using 'this'

		MainClass main = new MainClass(this);
		button.addActionListener(main);

//adding components
//setting size, layout and visibility  		add(b);
		add(field);
		setSize(300, 300);
		setLayout(null);
		setVisible(true);

	}

	public static void main(String args[]) {
		new EventApp2();
	}
}

MainClass.java

package eventHandling;

import java.awt.event.*;

class MainClass implements ActionListener {
	EventApp2 obj;

	MainClass(EventApp2 obj) {
		this.obj = obj;
	}

	public void actionPerformed(ActionEvent e) {
		obj.field.setText("You clicked the button");
	}
}

Output:

java example

Example : Event handling by implementing ActionListener in anonymousΒ class

package eventHandling;

import java.awt.*;
import java.awt.event.*;

public class EventApp3 extends Frame {
	
	TextField field;

	EventApp3() {
		
		field = new TextField();
		field.setBounds(60, 50, 170, 20);
		Button button = new Button("click!!!");
		button.setBounds(50, 120, 80, 30);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				field.setText("Helloooo");
			}
		});
		
		add(button);
		add(field);
		setSize(300, 300);
		setLayout(null);
		setVisible(true);
		
	}

	public static void main(String args[]) {
		new EventApp3();
	}
}

Output:

Java event handling example

Further Readings:

Conclusion

Event Handling in Java is very important mechanism.

It is the process of handling events in Java. Events are simply change of state of objects.

The management and listening of these events is known as Event Handling.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply