Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Wednesday, April 16, 2025

Event Handling in Java (Core Java GUI Programming)

 Introduction to AWT Event Handling

Event handling in Java refers to the mechanism that controls the event and decides what should happen if an event occurs. Java provides a powerful and flexible event handling mechanism as part of the AWT (Abstract Window Toolkit).

🧠 Basic Terminologies:

  • Event: An object that describes a state change in a source.
  • Event Source: The component (like button, window, etc.) that generates an event.
  • Event Listener: An interface in the java.awt.event package which listens to events and provides appropriate response.

🔶 Basics of Event Handling

🔸 Steps for Event Handling:

  1. Implement the listener interface.
  2. Register the listener with a component using addXXXListener() method.
  3. Override the methods of the listener interface to define event-specific behavior.

🔸 Commonly Used Event Classes:

Event Class

Description

ActionEvent

        Generated when a button is clicked

MouseEvent

        Generated on mouse actions

WindowEvent

        Window open, close, minimize


🔶 Event Handlers in AWT

Event Handlers are classes that implement specific listener interfaces and override their methods to respond to events.

 Syntax:

Component.addXxxListener(new XxxListener() {

    public void methodName(Event e) {

        // code to handle event

    }

});

 Example: Handling Button Click using ActionListener


Program:


import java.awt.*;

import java.awt.event.*;

public class ButtonClickExample extends Frame implements ActionListener {

    Button b;

    ButtonClickExample() {

        b = new Button("Click Me");

        b.setBounds(100, 100, 80, 30);

        b.addActionListener(this);  // Register event handler

        add(b);

        setSize(300, 300);

        setLayout(null);

        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {

        System.out.println("Button Clicked");

    } 

    public static void main(String[] args) {

        new ButtonClickExample();

    }

}


Output:







🔶 Adapter Classes

📌 Why Adapter Classes?

  • When a listener interface contains multiple abstract methods, and you only need to override one or two, adapter classes help reduce boilerplate.

 Common Adapter Classes:

Listener Interface

Adapter Class

MouseListener

MouseAdapter

KeyListener

KeyAdapter

WindowListener

WindowAdapter

 Example: Using WindowAdapter

import java.awt.*;

import java.awt.event.*;

 

public class WindowCloseExample extends Frame {

    WindowCloseExample() {

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                System.out.println("Window Closing");

                dispose();

            }

        });

 

        setSize(300, 300);

        setLayout(null);

        setVisible(true);

    }

 

    public static void main(String[] args) {

        new WindowCloseExample();

    }

}


🔶 Action Events

📌 Generated when:

  • Button is clicked
  • Menu item is selected
  • A text field triggers an action

 Method in ActionListener:

void actionPerformed(ActionEvent e);

 Example:

TextField tf;

Button b;

 

b.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

        tf.setText("Action Detected");

    }

});


🔶 Mouse Events

Mouse events are triggered by mouse actions like click, press, release, enter, and exit.

 Listener Interface:

interface MouseListener {

    void mouseClicked(MouseEvent e);

    void mousePressed(MouseEvent e);

    void mouseReleased(MouseEvent e);

    void mouseEntered(MouseEvent e);

    void mouseExited(MouseEvent e);

}

 Example Using MouseAdapter:

import java.awt.*;

import java.awt.event.*;

 

public class MouseExample extends Frame {

    Label label;

 

    MouseExample() {

        label = new Label();

        label.setBounds(20, 50, 200, 30);

        add(label);

 

        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                label.setText("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");

            }

        });

 

        setSize(300, 300);

        setLayout(null);

        setVisible(true);

    }

 

    public static void main(String[] args) {

        new MouseExample();

    }

}


OUTPUT:







📘 Summary Chart

Component/Event

Listener Interface

Adapter Class

Method

Button Click

ActionListener

N/A

actionPerformed()

Window Closing

WindowListener

WindowAdapter

windowClosing()

Mouse Events

MouseListener

MouseAdapter

mouseClicked()mouseEntered() etc.


🏁 Conclusion

AWT event handling provides a low-level mechanism for GUI interaction, suitable for building lightweight graphical user interfaces. Understanding the event model, listener interfaces, and adapter classes is essential for effective Java GUI programming. Adapter classes significantly reduce code clutter and are recommended for clean and readable programs.


Would you like me to format this in a downloadable PDF or PowerPoint format for classroom use, Krishna?

 


No comments:

Post a Comment