Advance Java Chapter 3 Full Notes - Ur Engineering Friend
Advance Java Chapter 3 Full Notes - Ur Engineering Friend
MSBTE Diploma
Please visit our Youtube Channel for more MSBTE Content. Click here !
The Event Delegation Model in Java is a design pattern used to handle events (such as
mouse clicks, key presses, etc.) in graphical user interfaces (GUIs) built using AWT or
Swing. This model provides an efficient way to manage event handling by delegating the
responsibility of responding to events to designated listeners.
1. Event Source:
o The component or object that generates an event. For example, a button
(JButton) or a text field (JTextField) can be event sources.
o When the user interacts with the component (e.g., clicks a button), an event is
triggered.
2. Event Object:
o A class representing the event that occurs. It encapsulates details about the
event, such as the type of event and the source of the event.
o Example event classes: ActionEvent, MouseEvent, KeyEvent.
3. Event Listener:
o An interface or class that listens for specific events and responds to them.
o An event listener is registered with an event source and contains methods that
define how to respond when an event occurs.
o Example listener interfaces: ActionListener, MouseListener, KeyListener.
4. Event Handling Process:
1. Event Source:
o The component that generates events.
o Examples: Button, text field, window.
2. Event Object:
o The class that represents the event.
o Contains information about the event, such as the event source, the type of
event, and any associated data.
o Examples: ActionEvent, MouseEvent, KeyEvent.
3. Event Listener:
o An interface or class that receives and handles events.
o The listener contains methods that respond to specific events (e.g.,
actionPerformed() for button clicks).
o Examples: ActionListener, MouseListener.
4. Event Handling Code:
o Code inside the event listener methods that defines how the application should
respond to the event.
o For example, in an ActionListener, the actionPerformed() method might
display a message when a button is clicked.
1. Registering a Listener:
o The event source must register an event listener to handle events. This is done
using the addXXXListener() method, where XXX corresponds to the event
type (e.g., addActionListener() for ActionEvent).
o Example: button.addActionListener(new ActionListener() { ... });
2. Event Occurrence:
Event Classes
In Java, event classes represent different types of events that can occur in a GUI-based
application. These events are triggered by user interactions with components like buttons, text
fields, windows, etc. Event classes contain information about the event, such as its source, the
type of event, and relevant data (e.g., mouse coordinates, key pressed, etc.).
Java uses the Event Delegation Model to handle events, where an event source generates
events and delegates their handling to registered listeners. Event classes encapsulate the
details of these interactions, making them easy to manage.
All event classes in Java are derived from the base class java.util.EventObject, which is the
root class for all events in Java.
• EventObject: The superclass of all event classes. It contains a reference to the event
source.
• AWTEvent: The subclass of EventObject and the base class for all Abstract Window
Toolkit (AWT) events. It provides support for events occurring in the AWT-based
GUI.
1. ActionEvent:
2. MouseEvent:
o Description: Represents mouse-related events like clicks, presses, releases,
movement, and drags.
o Use Case: Mouse clicks, dragging, entering, and exiting components.
o Methods:
▪ getX(), getY(): Returns the X and Y coordinates of the mouse event.
▪ getClickCount(): Returns the number of times the mouse was clicked.
▪ getButton(): Returns which mouse button was pressed (e.g., left or
right button).
o Example:
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked at: " + e.getX() + ", " + e.getY());
}
});
3. KeyEvent:
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyCode());
}
});
4. WindowEvent:
o Description: Represents events related to window operations such as opening,
closing, minimizing, and maximizing.
o Use Case: Closing a window, minimizing, restoring, etc.
o Methods:
▪ getWindow(): Returns the Window that generated the event.
o Example:
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window is closing.");
System.exit(0);
}
});
5. FocusEvent:
o Description: Represents events that occur when a component gains or loses
focus (e.g., switching between input fields).
o Use Case: Focus transitions between UI components like text fields.
addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
System.out.println("Component gained focus.");
}
});
6. ItemEvent:
o Description: Represents events generated by components like checkboxes and
radio buttons when the state changes.
o Use Case: Selection or deselection of an item.
o Methods:
▪ getStateChange(): Returns the state change (ItemEvent.SELECTED or
ItemEvent.DESELECTED).
o Example:
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println("Checkbox state changed.");
}
});
7. AdjustmentEvent:
o Description: Represents events from adjustable components like scrollbars.
o Use Case: Adjusting a scrollbar's position.
o Methods:
▪ getValue(): Returns the current value of the scrollbar.
o Example:
scrollbar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("Scrollbar adjusted to: " + e.getValue());
8. TextEvent:
o Description: Represents events related to changes in the text of a component,
like typing in a text field.
o Use Case: Text modification in text fields or text areas.
o Methods:
▪ getSource(): Returns the text component that triggered the event.
o Example:
textField.addTextListener(new TextListener() {
public void textValueChanged(TextEvent e) {
System.out.println("Text changed.");
}
});
Adapter Classes
Adapter classes in Java are special classes provided in the Abstract Window Toolkit (AWT)
and Swing frameworks that make it easier to handle events in GUI-based applications. These
classes are part of the Event Handling mechanism in Java's Event Delegation Model. They
provide default implementations for all the methods of an event listener interface, allowing
you to override only the methods you need.
• Simplify Event Handling: Many event listener interfaces have multiple methods that
you must implement even if you only need one or two of them. For example,
MouseListener has five methods, but often you only care about one, like
1. MouseAdapter:
o Adapts the MouseListener and MouseMotionListener interfaces. Provides
empty implementations for the methods in these interfaces.
o Interfaces:
▪ MouseListener: Handles events such as mouse clicks, presses, and
releases.
▪ MouseMotionListener: Handles mouse movement and dragging
events.
o Example methods:
▪ mouseClicked(), mouseEntered(), mouseExited(), mousePressed(),
mouseReleased().
▪ mouseMoved(), mouseDragged().
2. KeyAdapter:
o Adapts the KeyListener interface, providing empty implementations for its
methods.
o Interface:
Let’s look at how you can use the MouseAdapter class. In this example, we'll handle a
mouse click event without implementing the other MouseListener methods.
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this example, you have to implement all the methods in MouseListener, even if you only
care about mouseClicked().
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Here, using MouseAdapter, you only need to override the mouseClicked() method and can
ignore the others. This approach is much cleaner.
1. KeyAdapter Example:
import java.awt.event.*;
import javax.swing.*;
frame.add(textField);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
2. WindowAdapter Example:
import java.awt.event.*;
import javax.swing.*;
Event listener interfaces in Java are part of the event-handling mechanism provided by the
Abstract Window Toolkit (AWT) and Swing frameworks. These interfaces define methods
that must be implemented to respond to different types of events in a graphical user interface
(GUI), such as button clicks, mouse movements, or key presses. Event listeners are based on
the Event Delegation Model, which separates the logic for event generation from the logic
for event handling.
• Source: The component that generates an event (e.g., a button being clicked).
• Listener: The object that listens for and handles the event.
• Event: The actual event object that gets passed from the source to the listener (e.g., an
ActionEvent for a button click).
1. ActionListener
Example:
import javax.swing.*;
import java.awt.event.*;
2. MouseListener
• Example use case: Detecting when a user clicks a button or hovers over a component.
• Methods:
o mouseClicked(MouseEvent e) – Called when the mouse is clicked.
o mousePressed(MouseEvent e) – Called when a mouse button is pressed.
o mouseReleased(MouseEvent e) – Called when a mouse button is released.
o mouseEntered(MouseEvent e) – Called when the mouse enters a component.
o mouseExited(MouseEvent e) – Called when the mouse exits a component.
Example:
import java.awt.event.*;
import javax.swing.*;
3. KeyListener
Example:
import java.awt.event.*;
import javax.swing.*;
4. WindowListener
Example:
import java.awt.event.*;
import javax.swing.*;
frame.setSize(300, 200);
frame.setVisible(true);
}
}
5. FocusListener
Example:
import javax.swing.*;
import java.awt.event.*;