How To Implement Event handling In Java?

Last updated on Jun 17,2021 12.7K Views

How To Implement Event handling In Java?

edureka.co

While using different programs on your PC or android mobile applications, ever wondered what code is executed after clicking a button or a switch? Most of the programs and mobile applications are written in Java. Java has special ways of handling these situations know as event handling. In this post, we will dive deeper into the concept of Event Handling in Java and understand it’s working in great detail.

So lets us get started then,

Event Handling in Java

Source and Events

While understanding the concept of event handling you might have come across terms such as sources, events, etc. Sources and events are some of the basic terms which are to be understood before we look at event handling.

Event 

When you press a button in your program or Android application the state of the button changes from ‘Unclicked’ to ‘Clicked’.This change in the state of our button is called an Event. Events are generated based on how you interact with the GUI. For example- entering some text through the keyboard, moving your cursor, scrolling, etc generates events.

Source 

In Java, nearly everything is an object. The button you press is an object too. Sorce is the object which generates an event. In other words, a source is an object which undergoes state change. It also provides information about the event to the listener. We will talk about the listener in the other half of this post.

Now that we know what is source and event, lets move on to the next part of this event handling in Java article,

Listeners

Now we know about the events and the sources. This is a good time to talk about the listeners. Listeners are also called as event handlers as they are the ones responsible to handle events occurring at the source. Listeners are interfaces and different types of listeners are used according to the event.

For the understanding purpose, we will be looking at the ActionListener as it is the most commonly used event listener and see how exactly it handles the events.


import java.awt.*;
import java.awt.event.*;
class  EventHandle extends Frame implements ActionListener{
TextField textField;
EventHandle()
{
textField = new TextField();
textField.setBounds(60,50,170,20);
Button button = new Button("Quote");
button.setBounds(90,140,75,40);
//1
button.addActionListener(this);
add(button);
add(textField);
setSize(250,250);
setLayout(null);
setVisible(true);
}
//2
public void actionPerformed(ActionEvent e){
textField.setText("Keep Learning");
}
public static void main(String args[]){
new EventHandle();
}
}

                            Output

                                                  (1)                                                                                            (2)

Image 1 shows the output of our code when the state of the button was unclicked. Image 2 shows the output after the button is pressed.

Let’s us continue with event handling in java article and look at the logic behind the code and understand ActionListener in detail. 

First of all, we imported all the important packages required to implement the functionalities required. After importing packages we implemented ActionListener interface to our class EventHandle. 

Now, look at the code I’ve divided it into 2 important parts. Int the first part we are registering our button object with the ActionListener. This is done by calling the addActionListener( ) method and passing current instance using ‘this’ keyword.

button.addActionListener(this);

Once we have registered our button with the ActionListener now we need to override the actionPerformed( ) method which takes an object of class ActionEvent.

The code written in this method is executed when an event occurs. Hence we can say that this method performs a key role in the event handling process. Next in this event handling in Java article let us take a lookm at some event handlers,

List Of Listeners

Event

Methods to ‘Override’

EvenListener

ActionEvent- Events generated from buttons, menu items, etc.

actionPerformed(ActionEvent e)

ActionListener

KeyEvent- Events generaated when input is received from the keyboard.

keyPressed(KeyEvent ke)

keyTyped(KeyEvent ke)

keyReleased(KeyEvent ke)

KeyListener

ItemEvent- Events generated from List, Radio Button, etc.

itemStateChanged(ItemEvent ie )

ItemListener

MouseEvent Event generated by the mouse

mouseMoved(MouseEvent me)

mouseDragged(MouseEvent me)

MouseMotionListener

This brings us to the final bit of this event handling in Java article,

Delegation Event Model

We know about Source, Listener, and Event. Now let’s look at the model which joins these 3 entities and make them work in sync. The delegation event model is used to accomplish the task. It consists of 2 components Source and listener. As soon as the source generates an event it is noticed by the listener and it handles the event at hand. For this action to happen the component or the source should be registered with the listener so that it can be notified when an event occurs.

The specialty of delegation Event Model is that the GUI component passes the event processing part to a completely separate set of code.

The method of handling events is fast and efficient. Thus we have come to an end of this article on ‘Event handling in Java in Java’. If you wish to learn more, check out the Java Certification Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES