0% found this document useful (0 votes)
6 views

Java Study Material Unit IV April - May 2025

The document provides a detailed overview of Java's Event Delegation Model (EDM), which is essential for handling events in GUI programming. It explains key components like events, event sources, and event listeners, as well as mouse and keyboard event handling through specific interfaces and classes. Additionally, it discusses adapter classes and inner classes in Java, highlighting their roles in simplifying event handling and enhancing encapsulation.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Study Material Unit IV April - May 2025

The document provides a detailed overview of Java's Event Delegation Model (EDM), which is essential for handling events in GUI programming. It explains key components like events, event sources, and event listeners, as well as mouse and keyboard event handling through specific interfaces and classes. Additionally, it discusses adapter classes and inner classes in Java, highlighting their roles in simplifying event handling and enhancing encapsulation.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA STUDY MATERIAL

UNIT - IV

1. Explain in details about Event Delegation Model (EDM)


• The
he Delegation Event model is defined to handle events in GUI programming languages. languages The GUI stands for
Graphical User Interface, where a user graphically/visually interacts with the system.
• The GUI programming is inherently event-driven;
event driven; whenever a user initiates an activity such as a mouse activity,
clicks, scrolling, etc., each is known as an event that is mapped to a code to respond to functionality to the user. This
is known as event handling.

Event Processing in Java

Event Model is based on the following three components:

• Events
• Events Sources
• Events Listeners

Events:
• The Eventss are the objects that define state change in a source. An event can be generated as a reaction of a user
while interacting with GUI elements.
• Some of the event generation activities are moving the mouse pointer, clicking on a button, pressing the
keyboard key, selecting an item from the list, and so on. We can also consider many other user operations as
events.

Event Sources
• A source is an object that causes and generates an event. It generates an event when the internal state of the object
is changed. The sources are allowed to generate several different types of events.
• A source must register a listener to receive notifications for a specific event. Each event contains its registration
method.

event registration example:

public void addTypeListener (TypeListener e1)

Event Listeners
• An event listener is an object that is invoked
invoked when an event triggers. The listeners require two things; first, it
must be registered with a source; however, it can be registered with several resources to receive notification
about the events. Second, it must implement the methods to receive and process the received notifications.
• The methods that deal with the events are defined in a set of interfaces. These interfaces can be found in the
java.awt.event package.
Types of Events

The events are categories into the following two categories:

1. The Foreground Events:

The foreground events are those events that require direct interaction of the user. These types of events are generated as a
result of user interaction with the GUI component. For example, clicking on a button, mouse movement, pressing a
keyboard key, selecting an option from the list, etc.

2. The Background Events :

The Background events are those events that result from the interaction of the end-user. For example, an Operating
system interrupts system failure (Hardware or Software).

The Delegation Model

The Delegation Model is available in Java since Java 1.1. it provides a new delegation-based event model using AWT to
resolve the event problems. It provides a convenient mechanism to support complex Java programs.

Design Goals
The design goals of the event delegation model are as following:

• It is easy to learn and implement


• It supports a clean separation between application and GUI code.
• It provides robust event handling program code which is less error-prone (strong compile-time checking)
• It is Flexible, can enable different types of application models for event flow and propagation.
• It enables run-time discovery of both the component-generated events as well as observable events.
• It provides support for the backward binary compatibility with the previous model.
2. Explain in details about Mouse event handling in Java.
• Mouse event handling in Java is part of the AWT (Abstract Window Toolkit) and Swing frameworks. To handle
mouse events, you typically implement the MouseListener or MouseAdapter interface for basic mouse actions
(like click, press, release, enter, and exit), and/or the MouseMotionListener for mouse movements and drags.

• MouseListener:
This interface provides methods to handle basic mouse
vents: mousePressed, mouseReleased, mouseClicked, mouseEntered, and mouseExited.
• MouseMotionListener:
This interface handles mouse motion events, specifically mouseMoved and mouseDragged.
• MouseEvent:
This class encapsulates information about a mouse event, including the type of event, coordinates, and button
pressed.
• addMouseListener() and addMouseMotionListener():
These methods, available on Component objects, register listeners to receive mouse events.

Common Mouse Events and Their Methods:


Event Type Method in MouseListener Description

Mouse Button mousePressed(MouseEvent e) Invoked when a mouse button is pressed on a


Press component.

Mouse Button mouseReleased(MouseEvent e) Invoked when a mouse button is released on a


Release component.

Mouse Click mouseClicked(MouseEvent e) Invoked when a mouse button is clicked (pressed


and released) on a component.

Mouse Enter mouseEntered(MouseEvent e) Invoked when the mouse enters the area of a
component.

Mouse Exit mouseExited(MouseEvent e) Invoked when the mouse exits the area of a
component.

Mouse Motion mouseMoved(MouseEvent e) Invoked when the mouse is moved (without any
buttons pressed).

Mouse Drag mouseDragged(MouseEvent e) Invoked when a mouse button is pressed and the
mouse is moved (dragging the mouse)

Example: MouseListener with JFrame


import javax.swing.*; public void mousePressed(MouseEvent e) {
import java.awt.event.*; System.out.println("Mouse Pressed");
public class MouseExample extends JFrame implements MouseListener { }
public MouseExample() { public void mouseReleased(MouseEvent e) {
setTitle("Mouse Event Example"); System.out.println("Mouse Released");
setSize(400, 300); }
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void mouseEntered(MouseEvent e) {
addMouseListener(this); // Adding the listener System.out.println("Mouse Entered the window");
setVisible(true); }
} public void mouseExited(MouseEvent e) {
// MouseListener methods System.out.println("Mouse Exited the window");
public void mouseClicked(MouseEvent e) { }
System.out.println("Mouse Clicked at (" + e.getX() + ", " + e.getY() + public static void main(String[] args) {
")");} new MouseExample();
}}
3. Explain in details about Keyboard event handling in Java.
• keyboard events using the KeyEvent class and the KeyListener interface, which has methods
for keyPressed, keyReleased, and keyTyped to detect key presses, releases, and character input, respectively

1. Key Events and the KeyEvent Class:


• KeyEvent:
This class represents keyboard events, such as key presses, releases, and typed characters.
• Types of Events:
KEY_PRESSED: Occurs when a key is pressed.
KEY_RELEASED: Occurs when a key is released.
KEY_TYPED: Occurs when a character is typed (e.g., pressing 'a' or 'Shift' + 'a').
• Key Codes:
KeyEvent provides methods to retrieve the key code (e.g., VK_A, VK_SHIFT) and the character (e.g., 'a')
associated with the event.
Example:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyListenerExample implements KeyListener

@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}

@Override
public void keyReleased(KeyEvent e) {
System.out.println("Key Released: " + e.getKeyChar());
}

@Override
public void keyTyped(KeyEvent e) {
System.out.println("Key Typed: " + e.getKeyChar());
}
}

2. The KeyListener Interface:


• Purpose:
The KeyListener interface defines the methods for handling keyboard events.
• Methods:
keyPressed(KeyEvent e): Called when a key is pressed.
keyReleased(KeyEvent e): Called when a key is released.
keyTyped(KeyEvent e): Called when a character is typed.
Implementation:
To handle keyboard events, you need to:
o Create a class that implements the KeyListener interface.
o Override the keyPressed, keyReleased, and keyTyped methods to implement the desired behavior.
o Add the KeyListener instance to the component that you want to listen for keyboard events (e.g.,
a JFrame, JTextField, etc.) using addKeyListener(new MyKeyListener()).
Example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class KeyboardEventExample extends JFrame {

private JTextField textField;

public KeyboardEventExample() {
super("Keyboard Event Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLayout(new FlowLayout());

textField = new JTextField(20);


add(textField);

// Add a KeyListener to the text field


textField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("Key Typed: " + e.getKeyChar());
}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyCode());
}

@Override
public void keyReleased(KeyEvent e) {
System.out.println("Key Released: " + e.getKeyCode());
}
});

setVisible(true);
}

public static void main(String[] args)


{
SwingUtilities.invokeLater(KeyboardEventExample::new);
}
}

3. Key Differences between KeyEvent types:


• keyPressed and keyReleased:
These events provide information about the physical key press or release, including the key code and modifiers
(e.g., Shift, Ctrl).
• keyTyped:

This event provides the character that was typed, which is a higher-level representation of the keyboard input.
4. Explain in details about Adapter class in java.

Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter class, you will not be
forced to provide the implementation of all the methods of listener interfaces.

• It assists the unrelated classes to work combinedly.


• It provides ways to use classes in different ways.
• It increases the transparency of classes.
• It provides a way to include related patterns in the class.
• It provides a pluggable kit for developing an application.
• It increases the reusability of the class.

1. java.awt.event Adapter classes

dapter class Listener interface

WindowAdapter WindowListener

KeyAdapter KeyListener

MouseAdapter MouseListener

MouseMotionAdapter MouseMotionListener

FocusAdapter FocusListener

ComponentAdapter ComponentListener

ContainerAdapter ContainerListener

HierarchyBoundsAdapter HierarchyBoundsListener

2. java.awt.dnd Adapter classes

Adapter class Listener interface

DragSourceAdapter DragSourceListener

DragTargetAdapter DragTargetListener

3. javax.swing.event Adapter classes

Adapter class Listener interface

MouseInputAdapter MouseInputListener

InternalFrameAdapter InternalFrameListener
5. Explain in details about inner class in java.

• The inner classes, also known as nested classes, are classes defined within another class, used to logically group
classes and enhance encapsulation, particularly for event handling and adapter classes.

Purpose:

Inner classes are primarily used to group classes that are logically related and used within the context of the outer
class.

Access:

Inner classes have direct access to the members (including private ones) of the outer class.

Types of Inner class:

• Non-static (Regular) Inner Class: Associated with an instance of the outer class, meaning you need an instance of
the outer class to create an instance of the inner class.
• Static Nested Class: Not associated with an instance of the outer class and can only access static members of the
outer class.
• Local Inner Class: Defined within a block, typically a method.
• Anonymous Inner Class: A class without a name, used to instantiate objects with certain "on-the-fly" functionality.

AWT Event Handling with inner class:

Inner classes are frequently used to implement event listeners (e.g., MouseListener, ActionListener) in AWT, allowing
you to handle events related to user interface components.
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame {
public MyFrame() {
setTitle("Inner Class Example");
setSize(300, 200);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose(); // Close the frame
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MyFrame();
}
}

Benefits of Inner class:

• Encapsulation: Inner classes help in hiding implementation details and organizing code logically.
• Code Reusability: Inner classes can be used to implement specific functionalities that are used only
within the context of the outer class.
• Event Handling: Inner classes are particularly useful for handling events in AWT and other UI
frameworks

You might also like