CSC 2040 Week 11 More JavaFX and Events PDF

Summary

This document covers various aspects of JavaFX event handling, including processing, GUI validation, and different implementations of event handlers, such as anonymous inner classes and lambdas. It also discusses Java Code, and event handling using delegation classes. The document is suitable for undergraduate computer science courses focusing on programming.

Full Transcript

CSC 2040 MORE JAVAFX AND EVENTS OBJECTIVES AGENDA: WEEK 11 Describe the underlying architecture of 1. Processing Events how events are processed within 2. Overview Handling Events JavaFX applications. 3. Type of Events...

CSC 2040 MORE JAVAFX AND EVENTS OBJECTIVES AGENDA: WEEK 11 Describe the underlying architecture of 1. Processing Events how events are processed within 2. Overview Handling Events JavaFX applications. 3. Type of Events Explain the ways to provide event 4. GUI Validation handlers to handle the events 5. Scene Builder and Events generated as users interact within 6. Java Code and Events an application. 7. Anonymous in Java Explain why you might want to use a lambda expression instead of an 8. Demo anonymous inner class. 9. Property Event | Add Listeners Explain what functional interfaces are 10. TODO & Resources for Help and how you use them with lambda expressions. PROCESSING EVENTS Events are used to notify your GUI application of actions taken by the user and enable the application to respond to the event. The JavaFX platform provides the structure for capturing an event, routing the event to its target, and enabling the application to handle the event as needed. Associated with each event is an: 1. event source 2. event target 3. event type The javafx.event package provides the basic framework The Event class serves as the base class for JavaFX events. OVERVIEW HANDLING EVENTS (JAVADOCS) 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 operating system or windowing system captures generated events and notifies the listener objects when a type of event they are listening for has occurred. The operating system notifies the listener object by sending a message to (calling) the event handling method. If no listener object is subscribed to listen for the event that occurred, the event is ignored. TYPES OF EVENTS 1. ActionEvent 2. MouseEvent 3. DragEvent 4. KeyEvent 5. ScrollEvent 6. TouchEvent https://docs.oracle.com/javase/8/javafx/events-tutorial/img_text/event_type_hierarchy.htm GUI VALIDATION GUI applications validate once an event is triggered such as a button click, key press, mouse move/drag, change event, exception caught, etc. Inform user of failed validation through: Pop-up Alert – NOT recommended as users ignore and get annoyed with the constant pop-up alerts. Hidden label that is changed to visible upon error, displaying an error message Hidden imageView that is changed to visible upon error, with tool- tip error message upon hovering SCENE BUILDER AND EVENT HANDLING This article covers some of the most used events on common JavaFX controls using event handler properties. Setting an event handler property to a user-defined event handler automatically registers the handler to receive the corresponding event type: convienence methods JAVA CODE AND EVENT HANDLING 1. ViewController (this) Limitation if multiple different tasks for different events needs to be handled, as only the single handle() method for ViewController can be defined. 2. Private Inner / nested class: EventHandler II Separates out event details in inner class. Code is written more explicitly. 3. Anonymous Inner class: EventHandler III Event handler is coded within registration instruction and can be hard to decipher with the object instantation, class and method override all together 4. Lambda Expression: EventHandler IV Developer must understand functional interfaces and the API specifics to understand what method is being overriden and called. 5. Delegations class: EventHandler I - Separate Class Event handling is delegated to public helper class. Classes are passed objects of each other to communicate back and forth. The helper class can only access public fields and methods. VIEWCONTROLLER HANDLES EVENT Example of how we've generally been handling events: import javafx.scene.event.*; public class ViewController implements Initializable, EventHandler { @FXML private Button btn; @Override public void initialize(…) { btn.setOnAction(this); } // this class implemented the contract and is handling the event @Override public void handle(ActionEvent e) { … } } ANONYMOUS IN JAVA Anonymous simply means nameless. Anonymous object: An object which has no reference identifier. Anonymous objects can only be used at the time of object creation. If you have to use an object only once, an anonymous object is a good approach. Anonymous class: Anonymous inner class is an inner class without a name and for which only a single object is created. PRIVATE INNER CLASS HANDLES EVENT import javafx.scene.event.*; public class ViewController implements Initializable { @FXML private Button btn; @Override public void initialize(…) { btn.setOnAction(new BtnInnerHandler()); // anonymous object } private class BtnInnerHandler implements // private inner class EventHandler{ @Override public void handle(ActionEvent e) { … } } } ANONYMOUS INNER CLASS HANDLES import javafx.scene.event.*; public class ViewController implements Initializable { @FXML private Button btn; @Override public void initialize(…) { // EventHandler object is created and must implement handle btn.setOnAction(new EventHandler(){ @Override public void handle(ActionEvent e){ } // end of handle method – handle is auto called by the btn being clicked } // end of anonymous inner class definition ); // end of setOnAction method call } // end of initialize method defintion } // end of ViewController class definition LAMBDA -> EVENT import javafx.scene.event.*; public class ViewController implements Initializable { @FXML private Button btn; @Override public void initialize(…) { // where (e)-> represents @Override public void handle(ActionEvent e) btn.setOnAction((e) -> { … } // end of lambda ); // end of setOnAction method call } // end of method initialize definition } // end of class definition DELEGATION CLASS HANDLES EVENT import javafx.scene.event.*; public class ViewController implements Initializable { @FXML private Button btn; @Override public void initialize(…) { btn.setOnAction(new BtnDelegateHandler(this)); } public void printStatement(){ … } } class BtnDelegateHandler implements EventHandler { private ViewController app; public BtnDelegationHandler(ViewController vcApp) { app = vcApp; } @Override public void handle(ActionEvent e){ app.printStatement(); } } PROPERTY EVENTS | ADDLISTENER JavaFX properties provide an addListener method that lets you add event handlers that are called whenever the value of a property changes. You can create two types of property event handlers: A ChangeListener, is called whenever the value of the property has been recalculated. The change listener is passed three arguments: the property whose value has changed, the previous value of the property, and the new value. Event triggered from selection from a choice box, combo box or list item An InvalidationListener, is called whenever the value of the property becomes unknown. EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 11 Content Module in D2L to 100% WHAT'S COMING UP NEXT...WEEK 12 QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie Email: [email protected] RRCC On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help

Use Quizgecko on...
Browser
Browser