0% found this document useful (0 votes)
10 views18 pages

Event Listners

Uploaded by

22pw03
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)
10 views18 pages

Event Listners

Uploaded by

22pw03
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/ 18

JAVA

EVENT LISTNERS
UNDERSTANDING AND IMPLEMENTING EVENT LISTENERS

APARNA SATHYAMURTHI THAMBU


AND
20XW53 NOT HARIISH A
JAVA

DEFINITION
WHAT ARE EVENT LISTENERS ARE OBJECTS THAT LISTEN

EVENT
FOR AND RESPOND TO EVENTS OCCURRING IN
THE USER INTERFACE (UI).

LISTENERS? IN JAVAFX, THEY ARE DEFINED BY


INTERFACES THAT SPECIFY METHODS TO
HANDLE PARTICULAR TYPES OF EVENTS, SUCH
AS MOUSE CLICKS, KEY PRESSES, OR ACTIONS
TRIGGERED BY BUTTONS.

PURPOSE
THEY ENABLE APPLICATIONS TO REACT TO
USER INPUTS, MAKING THE UI INTERACTIVE
AND DYNAMIC.
JAVA

TYPES OF
ACTION EVENTS
EventHandler<ActionEvent>

EVENT MOUSE EVENTS


EventHandler<MouseEvent>

KEY EVENTS
EventHandler<KeyEvent>

WINDOW EVENTS
EventHandler<WindowEvent>
JAVAFX SUPPORTS SEVERAL TYPES OF EVENTS THAT
CAN BE HANDLED BY EVENT LISTENERS.
JAVA

ACTION EVENTS

Triggered when an action occurs (e.g., button clicks).


Handled using the EventHandler<ActionEvent> interface.
Example
JAVA

ACTION EVENTS - METHOD

setOnAction(EventHandler<ActionEvent> value)
JAVA

MOUSE EVENTS

Triggered by mouse actions such as clicks, movements, or drags.


Handled using the EventHandler<MouseEvent> interface.
Example
JAVA

MOUSE EVENTS - METHODS


setOnMouseClicked(EventHandler<MouseEvent> value)
setOnMousePressed(EventHandler<MouseEvent> value)
setOnMouseReleased(EventHandler<MouseEvent> value)
setOnMouseEntered(EventHandler<MouseEvent> value)
setOnMouseExited(EventHandler<MouseEvent> value)
setOnMouseMoved(EventHandler<MouseEvent> value)
setOnMouseDragged(EventHandler<MouseEvent> value)
setOnScroll(EventHandler<ScrollEvent> value)
setOnDragDetected(EventHandler<MouseEvent> value)
JAVA

KEY EVENTS

Triggered by keyboard actions (e.g., key presses and releases).


Handled using the EventHandler<KeyEvent> interface.
Example
JAVA

KEY EVENTS - METHODS


setOnKeyPressed(EventHandler<KeyEvent> value)
setOnKeyReleased(EventHandler<KeyEvent> value)
setOnKeyTyped(EventHandler<KeyEvent> value)
JAVA

WINDOW EVENTS

Triggered by actions related to the window, such as resizing or


closing.
Handled using the EventHandler<WindowEvent> interface.
Example
JAVA

WINDOW EVENTS - METHODS

setOnCloseRequest(EventHandler<WindowEvent> value)
setOnShowing(EventHandler<WindowEvent> value)
setOnShown(EventHandler<WindowEvent> value)
setOnHiding(EventHandler<WindowEvent> value)
setOnHidden(EventHandler<WindowEvent> value)
setOnCloseRequest(EventHandler<WindowEvent> value)
JAVA

IMPLEMENTING EVENT LISTENERS

STEPS:
1. CREATE A UI COMPONENT.
2. REGISTER THE EVENT LISTENER WITH THE COMPONENT.
3. DEFINE THE EVENT HANDLER METHOD.
JAVA

IMPLEMENTING EVENT LISTENERS


JAVA

IMPLEMENTING EVENT LISTENERS

Packages

import javafx.stage.WindowEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.KeyEvent;

Note: The ActionEvent class is part of the javafx.event package, and it is


automatically available when you use JavaFX controls that trigger action events,
such as buttons, menu items, etc.
JAVA

EXAMPLE

Write a program that demonstrates handling multiple event types in


JavaFX:
Button click: Submits and clears text.
Window close: Prints a message when the window closes.
Mouse click: Detects and displays the click position on the label.
Key press: Updates the label with the pressed key in the text field.
JAVA

CONCLUSION

JavaFX makes it easy to associate specific actions with UI events


using event listeners. Each event can be tied to a specific
component, enables the application to respond dynamically to user
input.
JAVA

REFERENCE
ORACLE
WORKING WITH EVENT HANDLERS

TUTORIAL’S POINT
SCHILDT, HERBERT. JAVAFX - EVENT HANDLING
INTRODUCING JAVAFX 8
PROGRAMMING. ORACLE
MCGRAW-HILL HANDLING EVENTS

EDUCATION, 2015. JAVA POINT


EVENT HANDLERS

YOUTUBE
EVENT HANDLING | JAVAFX GUI TUTORIAL
FOR BEGINNERS
JAVA

THANK YOU,
ANY QUESTIONS?

20XW53

You might also like