CH - 15 Java FX
CH - 15 Java FX
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
Rahul Talreja
4
Rahul Talreja 4
5
Events
An event can be defined as a type of
signal to the program that something has
happened.
Rahul Talreja
6
Event Classes
Rahul Talreja
Selected User Actions and 7
Handlers
Rahul Talreja
8
The Delegation Model
Rahul Talreja
9
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
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
Rahul Talreja
14
ControlCircle
Rahul Talreja
15
✖ An anonymous inner class must implement all the abstract methods in the
superclass or in the interface.
Rahul Talreja
16
new SuperClassName/InterfaceName() {
// Implement or override methods in superclass or interface
// Other methods if necessary
}
Rahul Talreja
17
AnonymousHandlerDemo
Rahul Talreja
Simplifying Event Handing 18
(a) Anonymous inner class event handler (b) Lambda expression event handler
Rahul Talreja
19
Rahul Talreja
20
AnonymousHandlerDemo
Rahul Talreja
21
MouseEvent
MouseEventDemo
Rahul Talreja
22
KeyEventDemo
Rahul Talreja
23
Rahul Talreja
24
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
FadeTransitionDemo
Rahul Talreja
Timeline 29
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.
LoanCalculator
Rahul Talreja
33
Thanks!
Any questions?
You can find me at:
Wechat
Dingtalk
Rahul Talreja