8. JavaFX Event Handling
8. JavaFX Event Handling
1
§15.1 Introduction
Suppose you want
to develop a GUI
application to
calculate loan
payments:
The user should
be able type the
pertinent values into the corresponding boxes, and
then click “calculate”
How can our program tell when the “Calculate”
button has been pressed (clicked on), so we can
run the code to do the calculation?
§15.1 Introduction
Let’s run through a simple example to get a taste
of what’s involved. We’ll create a stage with two
buttons – “OK” and “Cancel”
When the “OK” button is clicked, we’ll output “OK
Button Clicked” on the console
When the “Cancel” button is clicked, we’ll output
“Cancel button clicked” on the console
§15.1 Introduction
So far, all we have written is procedural code
The program’s flow execution is determined by the
program’s structure (and perhaps its input)
In event-driven code, the user is responsible for
determining what happens next
GUIs use event-driven programming to respond
to events:
Clicking on a button
Selecting a check box or a radio button
Clicking on a scroll bar
§15.1 Introduction
To respond to a button click, you need to write
the code to process the button-clicking action.
The button is an event source object – where the
action originates.
You need to create an object capable of handling
the action (click) event on a button.
This object is called an event handler, as shown
in Figure 15.3 (below)
§15.1 Introduction
Not just any ol’ object can be a handler for an
action event. To be an action event handler:
1. The object must implement the interface
EventHandler<T extends Event>, which defines
the common behavior for all action handlers
2. The EventHandler object handler must be
registered with the event source object using the
source.setOnAction(handler) method
The EventHandler<ActionEvent> interface
contains the method handle(ActionEvent) for
processing the event -- your handler class must
override this method to respond to the event
§15.1 Introduction
Listing 15.1 (p. 587) is an event-driven application
that we can use to explore the components that
have to be in place for event-driven code to work
Although they both answer the phone and then provide some
service, if the event is “Somebody is robbing the bank”, I want a
“police” KIND of handler, rather than a “customer service” KIND
handler.
WHAT !?!
§15.3 Registering Handlers & Handling Events
So far, classes have contained fields and
methods, period.
“Anonymous array”
§15.5 Anonymous Inner Classes
An anonymous inner class is handled pretty
much the same way
Rather than having the definition of the class (the
code that makes up the class) one place,…
…a declaration of an instance variable and an
instantiation (with new) someplace else, just so
we can pass a reference to an instance of the
class to some other method, …
We can use an anonymous inner class.
§15.5 Anonymous Inner Classes
We have seen anonymous objects (such as
.setForeground(new Color(Color.RED)),
which perform an instantiation and pass the
reference to some method, rather than storing it
in a reference variable and passing that.
An anonymous inner class combines a class’s
definition and instantiation in one step:
§15.5 Anonymous Inner Classes
public void start(Stage primaryStage)
{
// Several Lines Omitted
btnEnlarge.setOnAction(new EnlargeHandlner());
}
The Stage is given a title (l. 53), and made visible (l. 55)
§15.7: Case Study: Loan Calculator
Line 49 registers a call to calculateLoanPayment as
the handler for the “Calculate” Button’s ActionEvent.