SlideShare uma empresa Scribd logo
1 de 47
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados (20)

JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Html table tags
Html table tagsHtml table tags
Html table tags
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Xslt
XsltXslt
Xslt
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
Lab manual asp.net
Lab manual asp.netLab manual asp.net
Lab manual asp.net
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Structure in C
Structure in CStructure in C
Structure in C
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Java beans
Java beansJava beans
Java beans
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 

Semelhante a 004 - JavaFX Tutorial - Event Handling

Event driven architecture
Event driven architectureEvent driven architecture
Event driven architectureVinod Wilson
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IISivaSankari36
 
Event bus for android
Event bus for androidEvent bus for android
Event bus for android丞廷 鄭
 
Testing Event Driven Architecture Presentation
Testing Event Driven Architecture PresentationTesting Event Driven Architecture Presentation
Testing Event Driven Architecture PresentationKnoldus Inc.
 
Testing Event Driven Architecture Presentation
Testing Event Driven Architecture PresentationTesting Event Driven Architecture Presentation
Testing Event Driven Architecture PresentationKnoldus Inc.
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
Event handling62
Event handling62Event handling62
Event handling62myrajendra
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intentPERKYTORIALS
 
20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anneyLearningTech
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxTadeseBeyene
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxTanzila Kehkashan
 

Semelhante a 004 - JavaFX Tutorial - Event Handling (20)

Event driven architecture
Event driven architectureEvent driven architecture
Event driven architecture
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
 
Events1
Events1Events1
Events1
 
Event bus for android
Event bus for androidEvent bus for android
Event bus for android
 
Testing Event Driven Architecture Presentation
Testing Event Driven Architecture PresentationTesting Event Driven Architecture Presentation
Testing Event Driven Architecture Presentation
 
Testing Event Driven Architecture Presentation
Testing Event Driven Architecture PresentationTesting Event Driven Architecture Presentation
Testing Event Driven Architecture Presentation
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Event handling62
Event handling62Event handling62
Event handling62
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney20141107 node js_eventemitterpattern_anney
20141107 node js_eventemitterpattern_anney
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
 
Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
 
EventHandling.pptx
EventHandling.pptxEventHandling.pptx
EventHandling.pptx
 
42c
42c42c
42c
 

Mais de Mohammad Hossein Rimaz

Mais de Mohammad Hossein Rimaz (7)

003 - JavaFX Tutorial - Layouts
003 - JavaFX Tutorial - Layouts003 - JavaFX Tutorial - Layouts
003 - JavaFX Tutorial - Layouts
 
002- JavaFX Tutorial - Getting Started
002- JavaFX Tutorial - Getting Started002- JavaFX Tutorial - Getting Started
002- JavaFX Tutorial - Getting Started
 
Java 9, JShell, and Modularity
Java 9, JShell, and ModularityJava 9, JShell, and Modularity
Java 9, JShell, and Modularity
 
Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
Continuous integration with Jenkins
Continuous integration with JenkinsContinuous integration with Jenkins
Continuous integration with Jenkins
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
 

Último

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Último (20)

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

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