×

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

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute 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.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.