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 :
- Event Classes.
- Listener Interfaces.
- Event Handling Components.
- 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 Class | Listener Interface |
---|---|
MouseEvent | MouseListener MouseMotionListener |
KeyEvent | KeyListener |
MouseWheelEvent | MouseWheelListener |
ActionEvent | ActionListener |
TextEvent | TextListener |
WindowEvent | WindowListener |
ContainerEvent | ContainerListener |
ComponentEvent | ComponentListener |
AdjustmentEvent | AdjustmentListener |
FocusEvent | FocusListener |
ItemEvent | ItemListener |
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 :
- Implement the interface in the class.
- 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 name | Method |
---|---|
Button | public void addActionListener(ActionListener a){} |
TextField | 1) public void addActionListener(ActionListener a){} 2) public void addTextListener(TextListener a){} |
CheckBox | public void addItemListener(ItemListener a){} |
List | 1) public void addActionListener(ActionListener a){} 2) public void addItemListener(ItemListener a){} |
MenuItem | public void addActionListener(ActionListener a){} |
Choice | public void addItemListener(ItemListener a){} |
TextArea | public void addTextListener(TextListener a){} |
Java Event Handling Code
Event Handling code can be placed in below mentioned places :
- In class.
- In other class.
- 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:




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:


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:


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!