×

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

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.