Ch- 15
Event-Driven Programming and
Animations
Rahul Talreja
2
Agenda
Event-driven programming
Handler classes using inner classes
Handler classes using anonymous inner classes
Event handling using lambda expressions
GUI application for a loan calculator
Rahul Talreja
3
Procedural vs. Event-
Driven Programming
Procedural programming is executed in
procedural order.
In event-driven programming, code is
executed upon activation of events.
Rahul Talreja
4
Handling GUI Events
Source object (e.g., button)
Listener object contains a method for processing
the event.
Rahul Talreja 4
5
Events
An event can be defined as a type of
signal to the program that something has
happened.
The event is generated by external user
actions such as mouse movements, mouse
clicks, or keystrokes.
Rahul Talreja
6
Event Classes
Rahul Talreja
Selected User Actions and 7
Handlers
Rahul Talreja
8
The Delegation Model
Rahul Talreja
9
The Delegation Model: Example
Button btOK = new Button("OK");
OKHandlerClass handler = new
OKHandlerClass();
btOK.setOnAction(handler);
Rahul Talreja
10
Example: First Version for
ControlCircle (no listeners)
Now let us consider to write a program that
uses two buttons to control the size of a circle.
ControlCircleWithoutEventHandling
Rahul Talreja
11
Inner Class Listeners
✖ A listener class is designed specifically to create a
listener object for a GUI component (e.g., a
button).
✖ It will not be shared by other applications. So, it
is appropriate to define the listener class inside
the frame class as an inner class.
Rahul Talreja
12
Inner Classes
✖ Inner class: A class is a member of another class.
✖ Advantages: In some applications, you can use an inner class
to make programs simple.
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.
ShowInnerClass
Rahul Talreja
13
Inner Classes, cont.
Rahul Talreja
14
Example: Second Version for ControlCircle
(with listener for Enlarge)
Now let us consider to write a program that
uses two buttons to control the size of a circle.
ControlCircle
Rahul Talreja
15
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.
Rahul Talreja
16
Anonymous Inner Classes (cont.)
✖ Inner class listeners can be shortened using
anonymous inner classes.
✖ An anonymous inner class is an inner class without
a name. It combines declaring an inner class and
creating an instance of the class in one step.
✖ An anonymous inner class is declared as follows:
new SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
}
Rahul Talreja
17
Anonymous Inner Classes (cont.)
AnonymousHandlerDemo
Rahul Talreja
Simplifying Event Handing 18
Using Lambda Expressions
✖ Lambda expression is a new feature in Java 8.
✖ Lambda expressions can be viewed as an anonymous
method with a concise syntax.
btEnlarge.setOnAction( btEnlarge.setOnAction(e -> {
new EventHandler<ActionEvent>() { // Code for processing event e
@Override });
public void handle(ActionEvent e) {
// Code for processing event e
}
}
});
(a) Anonymous inner class event handler (b) Lambda expression event handler
Rahul Talreja
19
Basic Syntax for a Lambda Expression
✖ The basic syntax for a lambda expression is either
(type1 param1, type2 param2, ...) -> expression
Or
(type1 param1, type2 param2, ...) -> { statements; }
Rahul Talreja
20
Single Abstract Method Interface (SAM)
✖ 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.
✖ So, for the compiler to understand lambda expressions, the interface must
contain exactly one abstract method.
✖ Such an interface is known as a functional interface, or a Single Abstract
Method (SAM) interface.
AnonymousHandlerDemo
Rahul Talreja
21
MouseEvent
MouseEventDemo
Rahul Talreja
22
The KeyEvent Class
KeyEventDemo
Rahul Talreja
23
The KeyCode Constants
Rahul Talreja
24
Example: Control Circle
with Mouse and Key
ControlCircleWithMouseAndKey
Rahul Talreja
25
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.
✖ Once the value is changed in the property, a listener is notified. The listener
class should implement the InvalidationListener interface, which uses the
invalidated(Observable o) method to handle the property value change.
Every binding property is an instance of Observable.
DisplayResizableClock
Rahul Talreja
26
Animation
JavaFX provides the Animation class with the core
functionality for all animations.
Rahul Talreja
PathTransition 27
PathTransitionDemo
FlagRisingAnimation
Rahul Talreja
FadeTransition 28
The FadeTransition class animates the change of the
opacity in a node over a given time.
FadeTransitionDemo
Rahul Talreja
Timeline 29
PathTransition and FadeTransition define specialized
animations. The Timeline class can be used to program
any animation using one or more KeyFrames. Each
KeyFrame is executed sequentially at a specified time
interval. Timeline inherits from Animation.
TimelineDemo
Rahul Talreja
Clock Animation 30
ClockAnimation
Rahul Talreja
Case Study: Bouncing Ball 31
BallPane
BounceBallControl
Rahul Talreja
32
Motivations
User enter a loan amount, annual
interest rate, and number of years and
click the Compute Payment button to
obtain the monthly payment and total
payment.
Use event-driven programming to
write the code to respond to the
button-clicking event.
LoanCalculator
Rahul Talreja
33
Thanks!
Any questions?
You can find me at:
Wechat
Dingtalk
Rahul Talreja