×

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    InterfaceMethod     Explanation
1ActionEventActionListeneractionperformedActionEvent signifies the occurrence of a component-defined function.
2AdjustmentEventAdjustmentListeneradjustmentValueChanged()An movable item, such as scrolling, causes modification events.
3ComponentEventComponentListenercomponentMoved() componentShown()An event occurs when a component moved or changes its visibility or size changes.
4containerEventContainerListenercomponentRemoved()The event occurs whenever an element is put into or taken out of a container.
5FocusEventfocusListenerfocusLost() 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

Types of Events in Java

Related Topics

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.