1/10/2019 JavaFX Events COSC 330.

Slides:



Advertisements
Similar presentations
Adding User Interactions actionscript 3.0. Common event-handling tasks Writing code to respond to events Stopping code from responding to events Working.
Advertisements

Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Graphical User Interface (GUI) A GUI allows user to interact with a program visually. GUIs are built from GUI components. A GUI component is an object.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
Welcome to CIS 083 ! Events CIS 068.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming.  Interface  Event Handling.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Module 4 Taking Control of the User Interface. Module Overview Sharing Logical Resources in an Application Creating Consistent User Interfaces by Using.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Object-Oriented Programming: Inheritance and Polymorphism.
Understanding Desktop Applications Lesson 5. Understanding Windows Forms Applications Windows Forms applications are smart client applications consisting.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Lecture 8: User Interface Components with Swing.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
CompSci 230 S Programming Techniques
Java FX: Scene Builder.
Welcome! Workshop 3 of 7.
Chapter 15 Event-Driven Programming and Animations
Control Circle Project Revisited
Event Loops and GUI Intro2CS – weeks
Applets.
Chapter 12 Event-Driven Programming
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Responding to Events Event Handling in Java
A First Look at GUI Applications
Introduction to JavaScript Events
Chapter Eleven Handling Events.
Understand Windows Forms Applications and Console-based Applications
Introduction to Events
Programming in Java Event Handling
Android 16: Input Events Kirk Scott.
JavaScript and Events.
Windows Desktop Applications
Event Driven Programming
EE 422C Java FX.
Chapter 15 Event-Driven Programming and Animations
C# Event Processing Model
Introduction to Computing Using Java
Event Driven Systems and Modeling
Object-Oriented Programming: Inheritance and Polymorphism
Events, Event Handlers, and Threads
Tonga Institute of Higher Education
Chapter 15 Event-Driven Programming and Animations Part 1
Chapter 15 Event-Driven Programming and Animations Part 2
Tom Link CTO, Universal Mind
Presentation transcript:

1/10/2019 JavaFX Events COSC 330

1 Processing Events Events 1/10/2019 Events An event represents an occurrence of something of interest to the application. Foreground and background events You can define your own event by extending the Event class. COSC 330

Event Properties Event type Target Source Type of event that occurred. 1/10/2019 Event type Type of event that occurred. 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. Source Origin of the event, with respect to the location of the event in the event dispatch chain. Mouse click a button, mouse is the source. The source changes as the event is passed along the chain. COSC 330

Event Types 1/10/2019 The class named Event of the package javafx.event is the base class for an event. An event type is an instance of the Event class. Event types further classify the events of a single event class. MouseEvent. KeyEvent. DragEvent. WindowEvent. COSC 330

Event Targets 1/10/2019 The target of an event can be an instance of any class that implements the EventTarget interface. The implementation of the buildEventDispatchChain creates the event dispatch chain that the event must travel to reach the target. The Window, Scene, and Node classes implement the EventTarget interface. Therefore, most of the elements in your user interface have their dispatch chain defined, enabling you to focus on responding to the events and not be concerned with creating the event dispatch chain. If you create a custom UI control that responds to user actions and that control is a subclass of Window, Scene, or Node, those classes inherit the implementation and your control is an event target through inheritance. COSC 330

Event Delivery Process 1/10/2019 Target selection Route construction Event capturing Event bubbling COSC 330

Target Selection Rules 1/10/2019 For key events, the target is the node that has focus. For mouse events, the target is the node at the location of the cursor. For swipe events, the target is the node at the center of the entire path of all of the fingers. For touch events, the default target for each touch point is the node at the location first pressed. If more than one node is located at the cursor or touch, the topmost node is considered the target. COSC 330

Route Construction 1/10/2019 COSC 330

Route Construction 1/10/2019 The initial event route is determined by the event dispatch chain that was created in the implementation of the buildEventDispatchChain() method of the selected event target. COSC 330

1/10/2019 The route can be modified as event filters and event handlers along the route process the event. If an event filter or event handler consumes the event at any point, some nodes on the initial route might not receive the event. COSC 330

Event Capturing Phase 1/10/2019 The event is dispatched by the root node of your application and passed down the event dispatch chain to the target node. If any node in the chain has an event filter registered for the type of event that occurred, that filter is called. When the filter completes, the event is passed to the next node down the chain. If a filter is not registered for a node, the event is passed to the next node down the chain. If no filter consumes the event, the event target eventually receives and processes the event. COSC 330

Event Bubbling Phase 1/10/2019 After the event target is reached and all registered filters have processed the event, the event returns along the dispatch chain from the target to the root node. If any node in the chain has a handler registered for the type of event encountered, that handler is called. When the handler completes, the event is returned to the next node up the chain. If a handler is not registered for a node, the event is returned to the next node up the chain. If no handler consumes the event, the root node eventually receives the event and processing is completed. COSC 330

Event Handling Process 1/10/2019 Event handling is provided by event filters and event handlers, which are implementations of the EventHandler interface. If you want an application to be notified when an event occurs, register a filter or a handler for the event. The primary difference between a filter and a handler is when each one is executed. COSC 330

Event Filters Executed during the event capturing phase. 1/10/2019 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. A node can register more than one filter. The order in which each filter is called is based on the hierarchy of event types. Filters for a specific event type are executed before filters for generic event types. The order in which two filters at the same level are executed is not specified. COSC 330

Event Handlers Executed during the event bubbling phase. 1/10/2019 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. A node can register more than one handler. The order in which each handler is called is based on the hierarchy of event types. Handlers for a specific event type are executed before handlers for generic event types. The order in which two handlers at the same level are executed is not specified, with the exception that handlers that are registered by the convenience methods are executed last. COSC 330

Consuming of an Event 1/10/2019 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. However, 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. Note that the default handlers for the JavaFX UI controls typically consume most of the input events. COSC 330

Adding and Removing Event Filter 1/10/2019 To add an event filter to a node, you need to register this filter using the method addEventFilter() of the Node class. //Creating the mouse event handler EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { System.out.println("Hello World"); circle.setFill(Color.DARKSLATEBLUE); } }; //Adding event Filter circle.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler); //Removing event Filter circle.removeEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler); COSC 330

Adding and Removing Event Handlers 1/10/2019 //Creating the mouse event handler EventHandler<javafx.scene.input.MouseEvent> eventHandler = new EventHandler<javafx.scene.input.MouseEvent>() { @Override public void handle(javafx.scene.input.MouseEvent e) { System.out.println("Hello World"); circle.setFill(Color.DARKSLATEBLUE); } }; //Adding the event handler circle.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, eventHandler); //Removing the event handler circle.removeEventHandler(MouseEvent.MOUSE_CLICKED, eventHandler); COSC 330

2 Working with Convenience Methods 1/10/2019 Some of the classes in JavaFX define event handler properties. By setting the values to these properties using their respective setter methods, you can easily register to an event handler. These methods are known as convenience methods. Most of these methods exist in the classes like Node, Scene, Window, etc., and they are available to all their sub classes. playButton.setOnMouseClicked(new EventHandler<MouseEvent>() { public void handle(MouseEvent event) { System.out.println("Hello World"); pathTransition.play(); } }); COSC 330

final Circle circle = new Circle(radius, Color. RED); circle final Circle circle = new Circle(radius, Color.RED); circle.setOnMouseEntered(new EventHandler<MouseEvent>() { public void handle(MouseEvent me) { System.out.println("Mouse entered"); } }); circle.setOnMouseExited(new EventHandler<MouseEvent>() { System.out.println("Mouse exited"); circle.setOnMousePressed(new EventHandler<MouseEvent>() { System.out.println("Mouse pressed"); 1/10/2019 COSC 330

final TextField textBox = new TextField(); textBox final TextField textBox = new TextField(); textBox.setPromptText("Write here"); textBox.setOnKeyPressed(new EventHandler<KeyEvent>() { public void handle(KeyEvent ke) { System.out.println("Key Pressed: " + ke.getText()); } }); textBox.setOnKeyReleased(new EventHandler<KeyEvent>() { System.out.println("Key Released: " + ke.getText()); 1/10/2019 COSC 330