Types of Events in Java
One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The actions are keypresses, button presses, page scrolls, and cursor movements.
Several event classes are included in the Java package java.awt.event.
The events may be divided into the following two groups:
- Events in the Foreground
- Events in the background
Events in the foreground
Occurrences that demand user engagement are considered to be in the attention. A user engages with GUI elements to produce these foreground events. Action will be called whenever a people click a link, changes the pointer and drags the scrollbar.
Events in the background
Events in the Background events don't need a client to engage with them. Against the backdrop, such events are easily created. Instances of background occurrences include OS failures, OS interruptions, operations conclusions, etc.
Model of the Delegation Event
The term "event handling" refers to a system for executing tasks and choosing what must take place once they occur. When processing events, Java adheres to the Delegation Event Model.
Suppliers and Listeners make up the Delegated Event Model.
Source
Events are produced from various sources, including buttons, checkboxes, lists, menu items, choices, scrollbars, etc.
Listeners
The listener receives the actions produced by the sender. Every single client is an application that is in charge of processing events.
To handle events, we must connect the origin with the listeners. Different group categories provide various registration options.
Must use the following syntax to register a resource also with the listener:
addTypeListener
The addActionListener() or addKeyListener() methods, for instance, are used to register Key or Action events, respectively.
Procedures for managing an event
- When the user clicks the button, an event is created.
- Therefore, an instance of the relevant event classes is immediately constructed and filled with data related to both the source and the event.
- The function of a specified listener class receives the event object.
- The technique is now carried out and ends.
Reminders regarding the listener
- We must create several listener interfaces to be able to write a listener class.
- These Listener interfaces contain a few public abstract effective service recovery that the Listener classes must satisfy.
- A class could serve as a listener class for something like a source object if it does not follow each of the established interfaces.
ActionEvent: Indicates when a button or list item has been touched on a visual. ActionListener is indeed a related listener. The following represent some of the most typical events in Java:
ContainerEvent: This class defines an event that affects the GUI's containers, such as when an object is added to or removed from the interfaces by the customer. ContainerListener is a related listener.
KeyEvent: This class corresponds to a pressing, keystroke, or key release event. KeyListener is indeed a comparable listener.
WindowEvent: This class defines a window-related event, for instance, when a window is opened, closed, or disabled. WindowListener is indeed a related listener.
MouseEvent: Describes every mouse-related event, such as when the mouse button is pushed or clicked. MouseListener is a comparable listener.
Note:
should note that several listeners and event providers can communicate with each other. For instance, when they are of the same kind, a single listener can register numerous events. This implies that the listener may manage all events together for just a comparable collection of components that carry out the same operation. Similarly, if something makes sense for the program's architecture, a single incident can be connected to numerous listeners (although that is less common).
The most popular Event types are as follows:
S.N0 | Event class | Interface | Method | Explanation |
1 | ActionEvent | ActionListener | actionperformed | ActionEvent signifies the occurrence of a component-defined function. |
2 | AdjustmentEvent | AdjustmentListener | adjustmentValueChanged() | An movable item, such as scrolling, causes modification events. |
3 | ComponentEvent | ComponentListener | componentMoved() componentShown() | An event occurs when a component moved or changes its visibility or size changes. |
4 | containerEvent | ContainerListener | componentRemoved() | The event occurs whenever an element is put into or taken out of a container. |
5 | FocusEvent | focusListener | focusLost() and focusGained() | The concentration actions focused, focusout, focusing, and blurred are examples. |
Example1: Event1.java
//this program is for event handling in java
//importing the required packages
import java.awt.*;
import java.awt.event.*;
public class Event1 extends Frame {
TextField textField;
Event()
{
// creation of the component
textField = new TextField();
// The setBounds function is applied to give the product's location and size.
textField.setBounds(30, 40, 182, 20);
Button buttons = new Button("Press Here");
buttons.setBounds(101, 121, 81, 31);
// hidden component registration with listeners
buttons.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// setting up the text to the feild
textField.setText("Hidden feild");
}
});
// adding the components
add(textField);
add(buttons);
// setting up the visibility
setVisible(true);
}
public static void main(String[] args)
{
new Event();
}
}
Output
