Java Study Material Unit IV April - May 2025
Java Study Material Unit IV April - May 2025
UNIT - IV
• 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 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 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.
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 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:
• 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.
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)
import java.awt.event.KeyEvent;
import java.awt.event.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());
}
}
public KeyboardEventExample() {
super("Keyboard Event Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLayout(new FlowLayout());
@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);
}
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.
WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
HierarchyBoundsAdapter HierarchyBoundsListener
DragSourceAdapter DragSourceListener
DragTargetAdapter DragTargetListener
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.
• 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.
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();
}
}
• 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