004 - JavaFX Tutorial - Event Handling

Brought to you by
Graphical User Interface in Java
Using JavaFX
Event Handling
1K. N. Toosi University of Technology
What is Event?
• An event represents an occurrence of something of interest to the application,
such as a mouse being moved or a key being pressed
• In JavaFX, an event is an instance of the javafx.event.Event class or any subclass of
Event.
• Every JavaFX event has:
• eventType
• source
• target
• JavaFX provides several events, including DragEvent, KeyEvent, MouseEvent,
ScrollEvent, and others. You can define your own event by extending the Event
class.
• Subclasses can define type specific properties:
• MouseEvent: x,y
• KeyEvent: code, character
2K. N. Toosi University of Technology
Event Properties:
3K. N. Toosi University of Technology
Property Description
Event Type Type of event that occurred.
Source Origin of the event, with respect to the location of the
event in the event dispatch chain.
The source changes as the event is passed along the chain.
Target Node on which the action occurred and the end node in
the event dispatch chain. The target does not change,
however if an event filter consumes the event during the
event capturing phase, the target will not receive the event.
Event Hierarchy:
4K. N. Toosi University of Technology
EventObject
Event
InputEvent
KeyEvent MouseEvent DragEvent TouchEvent SwipeEvent …
ActionEvent WindowEvent
https://docs.oracle.com/javafx/2/events/processing.htm
Event Type Hierarchy:
5K. N. Toosi University of Technology
Event.ANY
InputEvent.ANY
KeyEvent.ANY
KeyEvent.KEY_PRESSED
KeyEvent.KEY_RELEASED
KeyEvent.KEY_TYPED
MouseEvent.ANY MouseEvent.MOUSE_PRESSED
MouseEvent.MOUSE_RELEASED
ActionEvent.ACTION
WindowEvent.ANY
WindowEvent.WINDOW_SHOWING
WindowEvent.WINDOW_SHOWN
https://docs.oracle.com/javafx/2/events/processing.htm
Event Delivery Process:
• Event Target Selection
• Depends on the particular event type
• Event Route Construction
• Specified by the event target
• Event Capturing Phase
• Event travels from the stage to the event target
• Event Bubbling Phase
• Events travels back from the target to the stage
6K. N. Toosi University of Technology
Demo 1:
7K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
Event Delivery Process:
• Event Target Selection
• Depends on the particular event type
• Event Route Construction
• Specified by the event target
• Event Capturing Phase
• Event travels from the stage to the event target
• Event Bubbling Phase
• Events travels back from the target to the stage
8K. N. Toosi University of Technology
Demo 1: Click on Green Circle
9K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 1: Circle Select as the target as the
first phase of Event Delivery Process.
Event Delivery Process:
• Event Target Selection
• Depends on the particular event type
• Event Route Construction
• Specified by the event target
• Event Capturing Phase
• Event travels from the stage to the event target
• Event Bubbling Phase
• Events travels back from the target to the stage
10K. N. Toosi University of Technology
Demo 1: Click on Green Circle
11K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 2: Route construction happens in the
second phase from the scene graph.
Event Delivery Process:
• Event Target Selection
• Depends on the particular event type
• Event Route Construction
• Specified by the event target
• Event Capturing Phase
• Event travels from the stage to the event target
• Event Bubbling Phase
• Events travels back from the target to the stage
12K. N. Toosi University of Technology
Demo 1: Click on Green Circle
13K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 3: In the event capturing phase, the
created event traverse from the top of the
scene graph to the target node.
• EventHandlers will not invoke
Event
Demo 1: Click on Green Circle
14K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 3: In the event capturing phase, the
created event traverse from the top of the
scene graph to the target node.
• EventHandlers will not invoke
Event
Event Delivery Process:
• Event Target Selection
• Depends on the particular event type
• Event Route Construction
• Specified by the event target
• Event Capturing Phase
• Event travels from the stage to the event target
• Event Bubbling Phase
• Events travels back from the target to the stage
15K. N. Toosi University of Technology
Demo 1: Click on Green Circle
16K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 4: In the Event Bubbling phase, the
Event traverse from the bottom of the
scene graph to the root.
• EventHandlers will invoke in this phase
Event
Demo 1: Click on Green Circle
17K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 4: In the Event Bubbling phase, the
Event traverse from the bottom of the
scene graph to the root.
• EventHandlers will invoke in this phaseEvent
Event Delivery Process:
• Event Target Selection
• Depends on the particular event type
• Event Route Construction
• Specified by the event target
• Event Capturing Phase
• Event travels from the stage to the event target
• Event Bubbling Phase
• Events travels back from the target to the stage
18K. N. Toosi University of Technology
Event Handling
• Three methods of event handling
• Convenience methods
• setOnKeyPressed(eventHandler);
• setOn …
• Event handler / filter registration methods
• addEventHandler(eventType, eventHandler);
• addEventFilter(eventType,eventFilter);
• Event dispatcher property
• Differ in Complexity and level of control
19K. N. Toosi University of Technology
Event Handlers
• An event handler is executed during the event bubbling phase.
• If an event handler for a child node does not consume the event, an
event handler for a parent node can act on the event after a child
node processes it and can provide common event processing for
multiple child nodes.
• Handlers that are registered for the type of event that occurred are
executed as the event returns through the node that registered the
handler.
20K. N. Toosi University of Technology
Demo 1: Click on Green Circle
21K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 4: In the Event Bubbling phase, the
Event traverse from the bottom of the
scene graph to the root.
• EventHandlers will invoke in this phase
Event
Event Filters
• An event filter is executed during the event capturing phase.
• An event filter for a parent node can provide common event
processing for multiple child nodes and if desired, consume the event
to prevent the child node from receiving the event.
• Filters that are registered for the type of event that occurred are
executed as the event passes through the node that registered the
filter.
22K. N. Toosi University of Technology
Demo 1: Click on Green Circle
23K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Phase 3: In the event capturing phase, the
created event traverse from the top of the
scene graph to the target node.
• EventHandlers will not invoke
Event
Consuming of an Event
• An event can be consumed by an event filter or an event handler at any
point in the event dispatch chain by calling the consume() method.
• This method signals that processing of the event is complete and traversal
of the event dispatch chain ends.
• Consuming the event in an event filter prevents any child node on the
event dispatch chain from acting on the event.
• Consuming the event in an event handler stops any further processing of
the event by parent handlers on the event dispatch chain.
• If the node that consumes the event has more than one filter or handler
registered for the event, the peer filters or handlers are still executed.
24K. N. Toosi University of Technology
Event Handling
Convenience methods
• on<EventType>
• onMousePressed, onKeyTyped, onAction
• Easy way to install, remove or replace an event handler
• Sufficient for most use cases
• Only one handler per event type
• For selected event types only
• No support for event filter registration
25K. N. Toosi University of Technology
Event Handling
Convenience methods
26K. N. Toosi University of Technology
Event Handling
Handler / Filter registration methods
• Allow to register multiple event handlers / filters for a signle event
type
• addEventHandler, addEventFilter methods
• Can Register one event handler / filter for a whole class of events
• All mouse events, all input events, all events
• Specific handlers / filters executed before the generic ones
• If you wrote two handler for two type of event which they are KeyEvent.KEY_TYPED and
InputEvent.ANY the more specified one which is KeyEvent will be called before the
handler of the generic one which is InputEvent
• Execution order of event handlers at the same level is unspecified and
not guaranteed
• Exception: on<EventType> handlers executed last
27K. N. Toosi University of Technology
Event Handling
Handler / Filter registration methods
28K. N. Toosi University of Technology
Handler or Filter?
• Different execution order
• Event handler for leaf nodes
• Event handler installed on a branch node
• Allows to define a default event response for all child nodes
• A child node can still define a more specific event response
• Event filter installed on a branch node
• Can override / force an event response
• Can block events from reaching their destination
29K. N. Toosi University of Technology
Demo 1:
30K. N. Toosi University of Technology
Demo 1:
31K. N. Toosi University of Technology
Demo 1:
32K. N. Toosi University of Technology
Demo 1:
33K. N. Toosi University of Technology
Demo 1:
34K. N. Toosi University of Technology
Demo 1:
35K. N. Toosi University of Technology
Demo 1: Click on Green Circle
36K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : Stage
• Event Target : Circle
• Executed : Stage Filter
Event
Demo 1: Click on Green Circle
37K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : Scene
• Event Target : Circle
• Executed : NothingEvent
Demo 1: Click on Green Circle
38K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : VBox
• Event Target : Circle
• Executed : VBox Filter
Event
Demo 1: Click on Green Circle
39K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : HBox
• Event Target : Circle
• Executed : HBox Filter
Event
Demo 1: Click on Green Circle
40K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : Circle
• Event Target : Circle
• Executed : Circle Filter
Event
Demo 1: Click on Green Circle
41K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : Circle
• Event Target : Circle
• Executed : Circle Handler
Event
Demo 1: Click on Green Circle
42K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : HBox
• Event Target : Circle
• Executed : HBox Handler
Event
Demo 1: Click on Green Circle
43K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : VBox
• Event Target : Circle
• Executed : VBox Handler
if not Consumed
Event
Demo 1: Click on Green Circle
44K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : VBox
• Event Target : Scene
• Executed : Nothing
if not Consumed
Event
Demo 1: Click on Green Circle
45K. N. Toosi University of Technology
Stage
Scene
VBox
Rectangle HBox
Circle Polygon
• Event Source : Stage
• Event Target : Circle
• Executed : Stage Handler
if not Consumed
Event
Resources & Refrences
• Oracle’s JavaFX: Handling Events Tutorial;
https://docs.oracle.com/javase/8/javafx/events-tutorial/events.htm
• JavaFX Event System Walk-through, Presented by eva krejčířová on
Oracle JavaOne 2012 conf;
https://www.youtube.com/watch?v=PNLNEzXcZE4
• Codes:
https://gist.github.com/mhrimaz/009afb0ffe444f478ceec5063dbbf277
46K. N. Toosi University of Technology
Brought to you by
Happy Coding 
47K. N. Toosi University of Technology
1 of 47

Recommended

Object Oriented Programming Concepts by
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
90.9K views14 slides
Event handling by
Event handlingEvent handling
Event handlingAnand Grewal
6.4K views34 slides
Strings in Java by
Strings in Java Strings in Java
Strings in Java Hitesh-Java
434 views21 slides
Object-oriented concepts by
Object-oriented conceptsObject-oriented concepts
Object-oriented conceptsBG Java EE Course
62.6K views41 slides
Cookie & Session In ASP.NET by
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NETShingalaKrupa
11.3K views20 slides
Java Server Pages by
Java Server PagesJava Server Pages
Java Server PagesKasun Madusanke
5.1K views35 slides

More Related Content

What's hot

Advanced Javascript by
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
8.2K views88 slides
Java Presentation by
Java PresentationJava Presentation
Java Presentationpm2214
17.4K views28 slides
Java Database Connectivity (JDBC) by
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
1.5K views46 slides
Java Server Pages(jsp) by
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)Manisha Keim
4.5K views31 slides
Jsp ppt by
Jsp pptJsp ppt
Jsp pptVikas Jagtap
22.2K views94 slides
Servlet and servlet life cycle by
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycleDhruvin Nakrani
3.6K views17 slides

What's hot(20)

Advanced Javascript by Adieu
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu8.2K views
Java Presentation by pm2214
Java PresentationJava Presentation
Java Presentation
pm221417.4K views
Java Database Connectivity (JDBC) by Pooja Talreja
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Pooja Talreja1.5K views
Java Server Pages(jsp) by Manisha Keim
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim4.5K views
Servlet and servlet life cycle by Dhruvin Nakrani
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
Dhruvin Nakrani3.6K views
Java Programming for Designers by R. Sosa
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
R. Sosa 3.1K views
Genesis and Overview of Java by Ravi_Kant_Sahu
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
Ravi_Kant_Sahu8K views
Basic of Multithreading in JAva by suraj pandey
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey561 views
JavaScript Control Statements I by Reem Alattas
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
Reem Alattas3.4K views

Similar to 004 - JavaFX Tutorial - Event Handling

Event driven architecture by
Event driven architectureEvent driven architecture
Event driven architectureVinod Wilson
351 views36 slides
event-handling.pptx by
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
1 view97 slides
Module 5.pptx by
Module 5.pptxModule 5.pptx
Module 5.pptxVeenaNaik23
5 views55 slides
PROGRAMMING IN JAVA- unit 4-part II by
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IISivaSankari36
420 views30 slides
Events1 by
Events1Events1
Events1Nuha Noor
888 views41 slides

Similar to 004 - JavaFX Tutorial - Event Handling(20)

Event driven architecture by Vinod Wilson
Event driven architectureEvent driven architecture
Event driven architecture
Vinod Wilson351 views
PROGRAMMING IN JAVA- unit 4-part II by SivaSankari36
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
SivaSankari36420 views
Event bus for android by 丞廷 鄭
Event bus for androidEvent bus for android
Event bus for android
丞廷 鄭1.7K views
tL20 event handling by teach4uin
tL20 event handlingtL20 event handling
tL20 event handling
teach4uin1K views
Event handling62 by myrajendra
Event handling62Event handling62
Event handling62
myrajendra391 views
B2. activity and intent by PERKYTORIALS
B2. activity and intentB2. activity and intent
B2. activity and intent
PERKYTORIALS522 views
20141107 node js_eventemitterpattern_anney by LearningTech
20141107 node js_eventemitterpattern_anney20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney
LearningTech321 views
Chap - 2 - Event Handling.pptx by TadeseBeyene
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
TadeseBeyene9 views
Introduction to GS1 EPCIS standard and Oliot EPCIS X (EPCIS v2.0 prototype) by Jaewook Byun
Introduction to GS1 EPCIS standard and Oliot EPCIS X (EPCIS v2.0 prototype)Introduction to GS1 EPCIS standard and Oliot EPCIS X (EPCIS v2.0 prototype)
Introduction to GS1 EPCIS standard and Oliot EPCIS X (EPCIS v2.0 prototype)
Jaewook Byun349 views
File Handling by Sohanur63
File HandlingFile Handling
File Handling
Sohanur6351 views

More from Mohammad Hossein Rimaz

003 - JavaFX Tutorial - Layouts by
003 - JavaFX Tutorial - Layouts003 - JavaFX Tutorial - Layouts
003 - JavaFX Tutorial - LayoutsMohammad Hossein Rimaz
2.6K views40 slides
Java 9, JShell, and Modularity by
Java 9, JShell, and ModularityJava 9, JShell, and Modularity
Java 9, JShell, and ModularityMohammad Hossein Rimaz
1.2K views60 slides
Quick introduction to scala by
Quick introduction to scalaQuick introduction to scala
Quick introduction to scalaMohammad Hossein Rimaz
1.3K views60 slides
Continuous integration with Jenkins by
Continuous integration with JenkinsContinuous integration with Jenkins
Continuous integration with JenkinsMohammad Hossein Rimaz
406 views28 slides
Introduction to Scala by
Introduction to ScalaIntroduction to Scala
Introduction to ScalaMohammad Hossein Rimaz
5.8K views83 slides
JavaFX in Action Part I by
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part IMohammad Hossein Rimaz
3.4K views47 slides

Recently uploaded

Top-5-production-devconMunich-2023.pptx by
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptxTier1 app
9 views40 slides
Electronic AWB - Electronic Air Waybill by
Electronic AWB - Electronic Air Waybill Electronic AWB - Electronic Air Waybill
Electronic AWB - Electronic Air Waybill Freightoscope
5 views1 slide
AI and Ml presentation .pptx by
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptxFayazAli87
14 views15 slides
Navigating container technology for enhanced security by Niklas Saari by
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas SaariMetosin Oy
15 views34 slides
EV Charging App Case by
EV Charging App Case EV Charging App Case
EV Charging App Case iCoderz Solutions
9 views1 slide
ADDO_2022_CICID_Tom_Halpin.pdf by
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdfTomHalpin9
5 views33 slides

Recently uploaded(20)

Top-5-production-devconMunich-2023.pptx by Tier1 app
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptx
Tier1 app9 views
Electronic AWB - Electronic Air Waybill by Freightoscope
Electronic AWB - Electronic Air Waybill Electronic AWB - Electronic Air Waybill
Electronic AWB - Electronic Air Waybill
Freightoscope 5 views
AI and Ml presentation .pptx by FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8714 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy15 views
ADDO_2022_CICID_Tom_Halpin.pdf by TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 views
Generic or specific? Making sensible software design decisions by Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
Top-5-production-devconMunich-2023-v2.pptx by Tier1 app
Top-5-production-devconMunich-2023-v2.pptxTop-5-production-devconMunich-2023-v2.pptx
Top-5-production-devconMunich-2023-v2.pptx
Tier1 app8 views
How Workforce Management Software Empowers SMEs | TraQSuite by TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuite
TraQSuite6 views
Ports-and-Adapters Architecture for Embedded HMI by Burkhard Stubert
Ports-and-Adapters Architecture for Embedded HMIPorts-and-Adapters Architecture for Embedded HMI
Ports-and-Adapters Architecture for Embedded HMI
Burkhard Stubert33 views
Airline Booking Software by SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta9 views
Understanding HTML terminology by artembondar5
Understanding HTML terminologyUnderstanding HTML terminology
Understanding HTML terminology
artembondar57 views
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... by NimaTorabi2
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
NimaTorabi216 views
predicting-m3-devopsconMunich-2023-v2.pptx by Tier1 app
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptx
Tier1 app12 views
Transport Management System - Shipment & Container Tracking by Freightoscope
Transport Management System - Shipment & Container TrackingTransport Management System - Shipment & Container Tracking
Transport Management System - Shipment & Container Tracking
Freightoscope 5 views
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 views

004 - JavaFX Tutorial - Event Handling

  • 1. Brought to you by Graphical User Interface in Java Using JavaFX Event Handling 1K. N. Toosi University of Technology
  • 2. What is Event? • An event represents an occurrence of something of interest to the application, such as a mouse being moved or a key being pressed • In JavaFX, an event is an instance of the javafx.event.Event class or any subclass of Event. • Every JavaFX event has: • eventType • source • target • JavaFX provides several events, including DragEvent, KeyEvent, MouseEvent, ScrollEvent, and others. You can define your own event by extending the Event class. • Subclasses can define type specific properties: • MouseEvent: x,y • KeyEvent: code, character 2K. N. Toosi University of Technology
  • 3. Event Properties: 3K. N. Toosi University of Technology Property Description Event Type Type of event that occurred. Source Origin of the event, with respect to the location of the event in the event dispatch chain. The source changes as the event is passed along the chain. Target Node on which the action occurred and the end node in the event dispatch chain. The target does not change, however if an event filter consumes the event during the event capturing phase, the target will not receive the event.
  • 4. Event Hierarchy: 4K. N. Toosi University of Technology EventObject Event InputEvent KeyEvent MouseEvent DragEvent TouchEvent SwipeEvent … ActionEvent WindowEvent https://docs.oracle.com/javafx/2/events/processing.htm
  • 5. Event Type Hierarchy: 5K. N. Toosi University of Technology Event.ANY InputEvent.ANY KeyEvent.ANY KeyEvent.KEY_PRESSED KeyEvent.KEY_RELEASED KeyEvent.KEY_TYPED MouseEvent.ANY MouseEvent.MOUSE_PRESSED MouseEvent.MOUSE_RELEASED ActionEvent.ACTION WindowEvent.ANY WindowEvent.WINDOW_SHOWING WindowEvent.WINDOW_SHOWN https://docs.oracle.com/javafx/2/events/processing.htm
  • 6. Event Delivery Process: • Event Target Selection • Depends on the particular event type • Event Route Construction • Specified by the event target • Event Capturing Phase • Event travels from the stage to the event target • Event Bubbling Phase • Events travels back from the target to the stage 6K. N. Toosi University of Technology
  • 7. Demo 1: 7K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon
  • 8. Event Delivery Process: • Event Target Selection • Depends on the particular event type • Event Route Construction • Specified by the event target • Event Capturing Phase • Event travels from the stage to the event target • Event Bubbling Phase • Events travels back from the target to the stage 8K. N. Toosi University of Technology
  • 9. Demo 1: Click on Green Circle 9K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 1: Circle Select as the target as the first phase of Event Delivery Process.
  • 10. Event Delivery Process: • Event Target Selection • Depends on the particular event type • Event Route Construction • Specified by the event target • Event Capturing Phase • Event travels from the stage to the event target • Event Bubbling Phase • Events travels back from the target to the stage 10K. N. Toosi University of Technology
  • 11. Demo 1: Click on Green Circle 11K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 2: Route construction happens in the second phase from the scene graph.
  • 12. Event Delivery Process: • Event Target Selection • Depends on the particular event type • Event Route Construction • Specified by the event target • Event Capturing Phase • Event travels from the stage to the event target • Event Bubbling Phase • Events travels back from the target to the stage 12K. N. Toosi University of Technology
  • 13. Demo 1: Click on Green Circle 13K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 3: In the event capturing phase, the created event traverse from the top of the scene graph to the target node. • EventHandlers will not invoke Event
  • 14. Demo 1: Click on Green Circle 14K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 3: In the event capturing phase, the created event traverse from the top of the scene graph to the target node. • EventHandlers will not invoke Event
  • 15. Event Delivery Process: • Event Target Selection • Depends on the particular event type • Event Route Construction • Specified by the event target • Event Capturing Phase • Event travels from the stage to the event target • Event Bubbling Phase • Events travels back from the target to the stage 15K. N. Toosi University of Technology
  • 16. Demo 1: Click on Green Circle 16K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 4: In the Event Bubbling phase, the Event traverse from the bottom of the scene graph to the root. • EventHandlers will invoke in this phase Event
  • 17. Demo 1: Click on Green Circle 17K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 4: In the Event Bubbling phase, the Event traverse from the bottom of the scene graph to the root. • EventHandlers will invoke in this phaseEvent
  • 18. Event Delivery Process: • Event Target Selection • Depends on the particular event type • Event Route Construction • Specified by the event target • Event Capturing Phase • Event travels from the stage to the event target • Event Bubbling Phase • Events travels back from the target to the stage 18K. N. Toosi University of Technology
  • 19. Event Handling • Three methods of event handling • Convenience methods • setOnKeyPressed(eventHandler); • setOn … • Event handler / filter registration methods • addEventHandler(eventType, eventHandler); • addEventFilter(eventType,eventFilter); • Event dispatcher property • Differ in Complexity and level of control 19K. N. Toosi University of Technology
  • 20. Event Handlers • An event handler is executed during the event bubbling phase. • If an event handler for a child node does not consume the event, an event handler for a parent node can act on the event after a child node processes it and can provide common event processing for multiple child nodes. • Handlers that are registered for the type of event that occurred are executed as the event returns through the node that registered the handler. 20K. N. Toosi University of Technology
  • 21. Demo 1: Click on Green Circle 21K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 4: In the Event Bubbling phase, the Event traverse from the bottom of the scene graph to the root. • EventHandlers will invoke in this phase Event
  • 22. Event Filters • An event filter is executed during the event capturing phase. • An event filter for a parent node can provide common event processing for multiple child nodes and if desired, consume the event to prevent the child node from receiving the event. • Filters that are registered for the type of event that occurred are executed as the event passes through the node that registered the filter. 22K. N. Toosi University of Technology
  • 23. Demo 1: Click on Green Circle 23K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Phase 3: In the event capturing phase, the created event traverse from the top of the scene graph to the target node. • EventHandlers will not invoke Event
  • 24. Consuming of an Event • An event can be consumed by an event filter or an event handler at any point in the event dispatch chain by calling the consume() method. • This method signals that processing of the event is complete and traversal of the event dispatch chain ends. • Consuming the event in an event filter prevents any child node on the event dispatch chain from acting on the event. • Consuming the event in an event handler stops any further processing of the event by parent handlers on the event dispatch chain. • If the node that consumes the event has more than one filter or handler registered for the event, the peer filters or handlers are still executed. 24K. N. Toosi University of Technology
  • 25. Event Handling Convenience methods • on<EventType> • onMousePressed, onKeyTyped, onAction • Easy way to install, remove or replace an event handler • Sufficient for most use cases • Only one handler per event type • For selected event types only • No support for event filter registration 25K. N. Toosi University of Technology
  • 26. Event Handling Convenience methods 26K. N. Toosi University of Technology
  • 27. Event Handling Handler / Filter registration methods • Allow to register multiple event handlers / filters for a signle event type • addEventHandler, addEventFilter methods • Can Register one event handler / filter for a whole class of events • All mouse events, all input events, all events • Specific handlers / filters executed before the generic ones • If you wrote two handler for two type of event which they are KeyEvent.KEY_TYPED and InputEvent.ANY the more specified one which is KeyEvent will be called before the handler of the generic one which is InputEvent • Execution order of event handlers at the same level is unspecified and not guaranteed • Exception: on<EventType> handlers executed last 27K. N. Toosi University of Technology
  • 28. Event Handling Handler / Filter registration methods 28K. N. Toosi University of Technology
  • 29. Handler or Filter? • Different execution order • Event handler for leaf nodes • Event handler installed on a branch node • Allows to define a default event response for all child nodes • A child node can still define a more specific event response • Event filter installed on a branch node • Can override / force an event response • Can block events from reaching their destination 29K. N. Toosi University of Technology
  • 30. Demo 1: 30K. N. Toosi University of Technology
  • 31. Demo 1: 31K. N. Toosi University of Technology
  • 32. Demo 1: 32K. N. Toosi University of Technology
  • 33. Demo 1: 33K. N. Toosi University of Technology
  • 34. Demo 1: 34K. N. Toosi University of Technology
  • 35. Demo 1: 35K. N. Toosi University of Technology
  • 36. Demo 1: Click on Green Circle 36K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : Stage • Event Target : Circle • Executed : Stage Filter Event
  • 37. Demo 1: Click on Green Circle 37K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : Scene • Event Target : Circle • Executed : NothingEvent
  • 38. Demo 1: Click on Green Circle 38K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : VBox • Event Target : Circle • Executed : VBox Filter Event
  • 39. Demo 1: Click on Green Circle 39K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : HBox • Event Target : Circle • Executed : HBox Filter Event
  • 40. Demo 1: Click on Green Circle 40K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : Circle • Event Target : Circle • Executed : Circle Filter Event
  • 41. Demo 1: Click on Green Circle 41K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : Circle • Event Target : Circle • Executed : Circle Handler Event
  • 42. Demo 1: Click on Green Circle 42K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : HBox • Event Target : Circle • Executed : HBox Handler Event
  • 43. Demo 1: Click on Green Circle 43K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : VBox • Event Target : Circle • Executed : VBox Handler if not Consumed Event
  • 44. Demo 1: Click on Green Circle 44K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : VBox • Event Target : Scene • Executed : Nothing if not Consumed Event
  • 45. Demo 1: Click on Green Circle 45K. N. Toosi University of Technology Stage Scene VBox Rectangle HBox Circle Polygon • Event Source : Stage • Event Target : Circle • Executed : Stage Handler if not Consumed Event
  • 46. Resources & Refrences • Oracle’s JavaFX: Handling Events Tutorial; https://docs.oracle.com/javase/8/javafx/events-tutorial/events.htm • JavaFX Event System Walk-through, Presented by eva krejčířová on Oracle JavaOne 2012 conf; https://www.youtube.com/watch?v=PNLNEzXcZE4 • Codes: https://gist.github.com/mhrimaz/009afb0ffe444f478ceec5063dbbf277 46K. N. Toosi University of Technology
  • 47. Brought to you by Happy Coding  47K. N. Toosi University of Technology