+ All Categories
Home > Documents > Event-Driven Programming · Event-driven programming •An event is an object created from an event...

Event-Driven Programming · Event-driven programming •An event is an object created from an event...

Date post: 08-Dec-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
36
Event-Driven Programming Introduction to Programming and Computational Problem Solving - 2 CSE 8B Lecture 17
Transcript
Page 1: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event-Driven Programming

Introduction to Programming and Computational Problem Solving - 2

CSE 8B

Lecture 17

Page 2: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Announcements

• Assignment 8 is due Dec 9, 11:59 PM• Quiz 8 is Dec 11• Educational research study

– Dec 11, weekly reflection– Dec 18, post-class survey

• Final exam is Dec 17• Course and Professor Evaluations (CAPE)

– https://cape.ucsd.edu/– Must by completed before Dec 14, 8:00 AM

• Reading– Chapter 15

CSE 8B, Fall 2020 2

Page 3: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event-driven programming

• An event is an object created from an event source

• You can write code to process events such as a button click, mouse movement, and keystrokes

CSE 8B, Fall 2020 3

Page 4: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Procedural programming vs. event-driven programming

• Procedural programming

– Code is executed in procedural order

• Event-driven programming

– Code is executed upon activation of events

CSE 8B, Fall 2020 4

Page 5: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event-related objects

• Event source object

– Where the action originates

• Event object

– Created from an event source

• Event handler object

– Processes the event

CSE 8B, Fall 2020 5

Event source object Event object Event handler object

Page 6: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event sources

• An event source is an object that can create an event

– An event can be defined as a type of signal to the program that some external action has happened

• For example

– Button is event source

– Button click action is the event it creates

CSE 8B, Fall 2020 6

Page 7: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Events

• An event is an object created from an event source

• An event object contains whatever propertiesare pertinent to the event

CSE 8B, Fall 2020 7

Page 8: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Events

• You can identify the source object of the event using the getSource() instance method in the EventObjectclass

• The subclasses of EventObject deal with special types of events

CSE 8B, Fall 2020 8

Page 9: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Mouse events

• A MouseEvent is fired whenever a mouse button is pressed, released, clicked, moved, or dragged

CSE 8B, Fall 2020 9

Page 10: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Key events

• A KeyEvent is fired whenever a key is pressed, released, or typed

CSE 8B, Fall 2020 10

Page 11: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

KeyCode constants

• Every key event has an associated code returned by the getCode() method in KeyEvent

CSE 8B, Fall 2020 11

Page 12: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event handlers

• Not all objects can handle events

• An object capable of handling an event is called an event handler

– The event handler processes the event

• An event handler must be an instance of an appropriate event-handling interface

• An event handler must be registered with anevent source object

CSE 8B, Fall 2020 12

Page 13: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event handlers

• The event handler must be an instance of the EventHandler<T extends Event> interface– <T extends Event> denotes that T is a generic

type that is a subtype of Event– This interface defines the common behavior for all

handlers

• The EventHandler<ActionEvent> interface contains the handle(ActionEvent) method for processing the action event– An event handler class must override this method to

respond to the event

CSE 8B, Fall 2020 13

Page 14: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event handlers

• The EventHandler object handler must be registered with the event source object source using the method source.setOnAction(handler)

• Firing an event means to create an event and delegate the handler to handle the event

CSE 8B, Fall 2020 14

Page 15: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Event handlers

• The EventHandler<ActionEvent>interface contains the handle(ActionEvent) method for processing the action event

• An event handler class must override this method to respond to the event

CSE 8B, Fall 2020 15

Page 16: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Example

public class HandleEvent extends Application {

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

// Create a pane and set its properties

...

Button btOK = new Button("OK");

OKHandlerClass handler1 = new OKHandlerClass();

btOK.setOnAction(handler1);

...

}

}

class OKHandlerClass implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

System.out.println("OK button clicked");

}

} CSE 8B, Fall 2020 16

Create event source

Create event handler

Event handler class

Handle event

Register event handler

Page 17: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Example

public class HandleEvent extends Application {

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

// Create a pane and set its properties

...

Button btOK = new Button("OK");

OKHandlerClass handler1 = new OKHandlerClass();

btOK.setOnAction(handler1);

...

}

}

class OKHandlerClass implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

System.out.println("OK button clicked");

}

} CSE 8B, Fall 2020 17

1. Click OK

Page 18: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Example

public class HandleEvent extends Application {

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

// Create a pane and set its properties

...

Button btOK = new Button("OK");

OKHandlerClass handler1 = new OKHandlerClass();

btOK.setOnAction(handler1);

...

}

}

class OKHandlerClass implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

System.out.println("OK button clicked");

}

} CSE 8B, Fall 2020 18

2. The JVM invokes the

handler class’s handle method

Page 19: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Example

CSE 8B, Fall 2020 19

Page 20: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Common user actions, source objects, and event types generated

CSE 8B, Fall 2020 20

Page 21: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Inner classes

• An event handler class designed specifically to create an event handler object for a graphical user interface (GUI) component (e.g., a button) is not be shared by other applications

– As such, it is appropriate to define the event handler class inside the class that uses it

CSE 8B, Fall 2020 21

Page 22: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Inner classes

• An inner class (or nested class) is a class defined within the scope of another class

• An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class

CSE 8B, Fall 2020 22

Page 23: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Inner classes

CSE 8B, Fall 2020 23

Page 24: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Inner classes

• An inner class can be declared public, protected, or private subject to the same visibility rules applied to a member of the class

• An inner class can be declared static

– A static inner class can be accessed using the outer class name

– A static inner class cannot access nonstaticmembers of the outer class

CSE 8B, Fall 2020 24

Page 25: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Inner classes

• Normally, you only define an inner class if it is only used by its outer class

• Outside the outer class, an inner class may be used just like a regular class

– Create an instance of an inner class using:

• If the inner class is nonstaticOuterClass.InnerClass innerObject = outerObject.new InnerClass();

• If the inner class is staticOuterClass.InnerClass innerObject = new OuterClass.InnerClass()

CSE 8B, Fall 2020 25

Page 26: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Inner classes

• Inner classes can make programs simple and concise

• An inner class supports the work of its containing outer class

• An inner class does not need its own sourcefile and is compiled into a class named OuterClassName$InnerClassName.class

• Inner classes also avoid class-naming conflicts

CSE 8B, Fall 2020 26

Page 27: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Anonymous inner classes

• An anonymous inner class is an inner class without a name

• It combines defining an inner class andcreating an instance of the class into one step

CSE 8B, Fall 2020 27

Page 28: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Anonymous inner classes

• An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause

• An anonymous inner class must implement all the abstract methods in the superclass or in the interface

• An anonymous inner class always uses the no-argconstructor from its superclass to create an instance– If an anonymous inner class implements an interface, then

the constructor is Object()

• An anonymous inner class is compiled into a class named OuterClassName$n.class, where n = 1, ...

CSE 8B, Fall 2020 28

Page 29: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Lambda expressions

• Lambda expressions can be viewed as an anonymous method with a concise syntax

• The basic syntax for a lambda expression is either(type1 param1, type2 param2, ...) -> expression

• or(type1 param1, type2 param2, ...) -> { statements; }

• The data type for a parameter may be explicitly declared or implicitly inferred by the compiler

• The parentheses can be omitted if there is only one parameter without an explicit data type

CSE 8B, Fall 2020 29

No semicolon

Page 30: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Lambda expressions

• The compiler treats a lambda expression as if it is an object created from an anonymous inner class

– The compiler processes a lambda expression in the following three steps

1. Identify the lambda expression type

2. Identify the parameter types

3. Identify statements

CSE 8B, Fall 2020 30

Page 31: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

btEnlarge.setOnAction(

new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) {

// Code for processing event e

}

}

});

(a) Anonymous inner class event handler

btEnlarge.setOnAction(e -> {

// Code for processing event e

});

(b) Lambda expression event handler

Lambda expressions

• Lambda expressions can be used to greatlysimplify code for event handling– For example

(ActionEvent e) -> { circlePane.enlarge(); }(e) -> { circlePane.enlarge(); }e -> { circlePane.enlarge(); }e -> circlePane.enlarge()

CSE 8B, Fall 2020 31

All four of these are equivalent

Page 32: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

btEnlarge.setOnAction(

new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) {

// Code for processing event e

}

}

});

(a) Anonymous inner class event handler

btEnlarge.setOnAction(e -> {

// Code for processing event e

});

(b) Lambda expression event handler

Lambda expressions

• ExamplebtEnlarge.setOnAction(

e -> {

// Process event e

}

);

CSE 8B, Fall 2020 32

1. The compiler recognizes the lambda expression type is an object of the EventHandler<ActionEvent> type, because the expression is an argument in the setOnAction method

2. The compiler recognizes e is a parameter type of the ActionEvent type, because the EventHandler<ActionEvent> interface defines the handle method with aparameter of the ActionEvent type

3. The compiler recognizes the code for processing event e are the statements in the handle method

Page 33: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Lambda expressions

• The statements in the lambda expression is all for that method

• If it contains multiple methods, the compiler will not be able to compile the lambda expression

• As such, for the compiler to understand lambda expressions, the interface must contain exactly one abstract method– Such an interface is known as a single abstract method

(SAM) interface

• Essentially, a lambda expression creates an object, and the object performs a function by invoking this single method

CSE 8B, Fall 2020 33

Page 34: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Single abstraction method (SAM) interface

• A SAM interface is also known as a functional interface

• An instance of a functional interface is knownas a functional object

• Since a lambda expression defines exactly one function, a lambda expression is also called a lambda function

CSE 8B, Fall 2020 34

Page 35: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Listeners for observable objects

• You can add a listener to process a value change in an observable object

• An instance of Observable is known as an observable object, which contains the addListener(InvalidationListener listener)method for adding a listener

• The listener class must implement the functional interfaceInvalidationListener to override the invalidated(Observable o) method for handling the value change– Once the value is changed in the Observable object, the

listener is notified by invoking its invalidated(Observable o) method

– Every binding property is an instance of Observable

CSE 8B, Fall 2020 35

Page 36: Event-Driven Programming · Event-driven programming •An event is an object created from an event source •You can write code to process events such as a button click, mouse movement,

Next Lecture

• Binary I/O

• Reading

– Chapter 17

CSE 8B, Fall 2020 36


Recommended