Slide 1
Slide 2
Slide 3

ActionEvent in java programming (V63)

No Comments

Using Events
An Event is a change in the state of an object that is caused by certain actions such as clicking a button, moving the cursor, pressing a key on the keyboard, scrolling a page, etc.
In Java, the package java.awt.event provides various event classes to handle these activities.

Classification of Events

 

Events in Java can be broadly classified into two types based on the way they are generated:

Foreground Events:
These are events that require user interaction to be generated. Examples of such events include button clicks, scrolling the scrollbar, moving the cursor, etc.

Background Events:
Events that do not require user interaction to be generated are known as background events. Examples of such events include operating system failures/interrupts, operation completion, etc.

Event Handling Mechanism
Event handling is a mechanism that allows a program to control events and determine what should happen when an event occurs. Java uses the Delegation Event Model to handle events. This model has two main components:

Source:
Events are generated from a source. There are various sources such as buttons, checkboxes, list, menu-item, choice, scrollbar, text components, windows, etc., which generate events.

Listeners:
Listeners are used to handle the events generated by the source. Each listener represents an interface that is responsible for handling events.


Registering the Source With Listener
To handle events, the source must be registered with a listener. Java provides specific methods for registering listeners based on the type of event.

Syntax:

For example
addKeyListener() for KeyEvent
addActionListener() for ActionEvent

Event Classes and Listener Interfaces
Java provides many types of event classes and their corresponding listener interfaces. The table below shows the most commonly used event classes and their associated listener interfaces.

Event Class Listener Interface Description
ActionEvent ActionListener An event that indicates a component-defined action occurred, such as clicking a button or selecting a menu item.
AdjustmentEvent AdjustmentListener An event generated by an adjustable object such as a Scrollbar.
ComponentEvent ComponentListener An event that indicates a component was moved, resized, or its visibility changed.
ContainerEvent ContainerListener An event generated when a component is added to or removed from a container.
FocusEvent FocusListener Focus-related events such as focus gained and focus lost.
ItemEvent ItemListener An event that indicates whether an item was selected or deselected.
KeyEvent KeyListener An event generated by keystrokes on the keyboard.
MouseEvent MouseListener
MouseMotionListener
Events generated by user interaction with the mouse (pointing device).
MouseWheelEvent MouseWheelListener An event that indicates the mouse wheel was rotated.
TextEvent TextListener An event that occurs when the text of an object changes.
WindowEvent WindowListener An event that indicates a change in the window’s state.

ActionEvent

ActionEvent is a class in Java that is used in event-driven programming, especially with GUI components such as buttons, menus, and other interactive components.
It represents an action that occurs when the user interacts with a component (for example, clicking a button or selecting a menu item).

Package:

Example 1:

Source Code:

Copied!
mport javax.swing.JOptionPane;
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        try {
        int num1 = Integer.parseInt(txtNum1.getText());
        int num2 = Integer.parseInt(txtNum2.getText());
        int sum = num1 + num2;

        JOptionPane.showMessageDialog(this, "Sum: " + sum);
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this, "Please enter valid numbers.");
    }
}                                      

Result:


Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top