Java Reference
In-Depth Information
Specifying the Calculate Button's Event Handler in Scene Builder
When you created the TipCalculator project with NetBeans, it preconfigured the class
TipCalculatorController as the controller for the GUI in TipCalculator.fxml . You
can see this in Scene Builder by selecting the scene's root node (i.e., the GridPane ), then
expanding the Inspector window's Code section. The controller class's name is specified in
the Controller class field. If you declare the method calculateButtonPressed in the con-
troller class before specifying the Calculate Button 's event handler in the FXML, when you
open Scene Builder it scans the controller class for methods prefixed with @FXML and allows
you to select from those methods to configure event handlers.
To indicate that calculateButtonPressed should be called when the user clicks the
Calculate Button , open TipCalculator.fxml in Scene Builder, then:
1. Select the Calculate Button .
2. In the Inspector window, expand the Code section.
3. Under On Action , select #calculateButtonPressed from the drop-down list and
save the FXML file.
When the FXMLLoader loads TipCalculator.fxml to create the GUI, it creates and regis-
ters an event handler for the Calculate Button 's ActionEvent . An ActionEvent 's handler
is an object of a class that implements the EventHandler<ActionEvent> interface, which
contains a handle method that returns void and receives an ActionEvent parameter. This
method, in turn, calls method calculateButtonPressed when the user clicks the Calcu-
late Button . The FXMLLoader performs similar tasks for every event listener you specified
via the Inspector window's Code section.
Calculating and Displaying the Tip and Total Amounts
Lines 46-51 calculate and display the tip and total amounts. Line 46 calls method getText
to get from the amountTextField the bill amount typed by the user. This String is passed
to the BigDecimal constructor, which throws a NumberFormatException if its argument
is not a number. In that case, line 55 calls amountTextField 's setText method to display
the message "Enter amount" in the TextField . Line 56 then calls method selectAll to
select the TextField 's text and line 57 calls requestFocus , which gives the TextField the
focus. This allows the user to immediately type a new value in the amountTextField with-
out having to first select its text. Methods getText , setText and selectAll are inherited
into class TextField from class TextInputControl (package javafx.scene.control ),
and method requestFocus is inherited into class TextField from class Node (package
javafx.scene ).
If line 46 does not throw an exception, line 47 calculates the tip by calling method
multiply to multiply the amount by the tipPercentage , and line 48 calculates the total
by calling method add to add the tip to the bill amount . Next lines 50 and 51 use the
currency object's format method to create currency-formatted String s representing the
tip and total amounts—these are displayed in tipTextField and totalTextField , respec-
tively.
TipCalculatorController 's initalize Method
Figure 25.21 presents class TipCalculatorController 's initialize method. When the
FXMLLoader creates an object of class TipCalculatorController , it determines whether
the class contains an initialize method with no parameters and, if so, calls that method
 
Search WWH ::




Custom Search