Pages

Monday, November 22, 2021

Java Event Handler - Events and Listeners Examples

1. Overview

In this tutorial, We'll learn how to work with event handlers in java.

How to add events to the actions of users and work with the listeners in java.

When you are working on GUI based projects using AWT or Applets then you might have seen the scenarios where you need to change the state of an object from one form to another.

For example, add action when a button is pressed or when the text is entered then enable another text box.

Java Event Handler - Events and Listeners Examples


2. Java Event classes and Interfaces


java.event.awt package contains all events and listeners interfaces.

Event classs --> Event listener

2.1 ActionEvent-->ActionListener
2.2 MouseEvent-->MouseListener and MouseMotionListener
2.3 MouseWheelEvent-->MouseWheelListener
2.4 KeyEvent-->KeyListener
2.5 ItemEvent-->ItemListener
2.6 TextEvent-->TextListener
2.7 AdjustmentEvent-->AdjustmentListener
2.8 WindowEvent-->WindowListener
2.9 ComponentEvent-->ComponentListener
2.10 ContainerEvent-->ContainerListener
2.11 FocusEvent-->FocusListener

3. Steps to add Events and Listeners - Example


First, we need to understand the use case and pick the right interface.

Now, we want to change the text field text when the button is clicked.

Step 1:

Implement the ActionListener interface.

Step 2:

Implement actionPerformed() method. Inside this, we need to change the text field contents using textField.setText() method.

Step 3:

Add the action listener created to the button. You need to call the button.addActionListener() method and pass the instance of ActionListener implementation object.

Look at the below example.

This program first displays the window and a text field with the default text "Enter your name". This window is displayed with a button.

When the button is pressed then this text box text has to be changed to the "hello developer".

Example
package com.javaprogramto.programs.events;

import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JaveEventHandlerExample {

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

class ButtonEvent extends Frame implements ActionListener {

	TextField textField;

	ButtonEvent() {

		textField = new TextField();
		textField.setText("Enter your name");
		textField.setBounds(70, 60, 180, 30);
		Button b = new Button("click me");
		b.setBounds(110, 130, 90, 40);

		b.addActionListener(this);

		add(b);
		add(textField);
		setSize(300, 300);
		setLayout(null);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		textField.setText("hello developer");
	}
}

Output:

java action event listener 1


java action event listener 2


4. Adding Listener from Custom class


In the above example, creating and adding action listeners are done within the same class but now let us see how to separate the action listener. How to attach the action listener to the button.

First, create a separate action listener class.
class ActionEventListner implements ActionListener {

	ButtonEvent buttonEvent;

	public ActionEventListner(ButtonEvent buttonEvent) {
		this.buttonEvent = buttonEvent;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		buttonEvent.textField.setText("hello developer");

	}
}

Create the main class which extends Frame and pass the ActionEventListener object to the button of addActionListner() method.
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JaveEventHandlerExample2 {

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

class ButtonEvent extends Frame {

	TextField textField;

	ButtonEvent() {

		textField = new TextField();
		textField.setText("Enter your name");
		textField.setBounds(70, 60, 180, 30);
		Button b = new Button("click me");
		b.setBounds(110, 130, 90, 40);

		b.addActionListener(new ActionEventListner(this));

		add(b);
		add(textField);
		setSize(300, 300);
		setLayout(null);
		setVisible(true);
	}

}

This program also produces the same output.

5. Conclusion


In this article, we've seen how to add the action listeners for an event in java using java event handler concept.


No comments:

Post a Comment

Please do not add any spam links in the comments section.