Java Reference
In-Depth Information
method receives an object that implements interface
ObservableValue
(line 7)—
that is, a value that generates an event when it changes.
•A
Button
's event handler receives an
ActionEvent
(line 8; package
javafx.event
),
which indicates that the
Button
was clicked. As you'll see in online Chapter 26,
many JavaFX controls support
ActionEvent
s.
• The annotation
FXML
(line 9; package
javafx.fxml
) is used in a JavaFX controller
class's code to mark instance variables that should refer to JavaFX components in
the GUI's FXML file and methods that can respond to the events of JavaFX com-
ponents in the GUI's FXML file.
• e
javafx.scene.control
(lines 10-12) contains many JavaFX control
classes, including
Label
,
Slider
and
TextField
.
As you write code with various classes and interfaces, you can use the the NetBeans
IDE's
Source > Organize Imports
command to let the IDE insert the
import
statements for
you. If the same class or interface name appears in more than one package, the IDE will
display a dialog in which you can select the appropriate
import
statement.
TipCalculatorController
's
static
Variables and Instance Variables
Lines 17-38 of Fig. 25.19 present class
TipCalculatorController
's
static
and instance
variables. The
NumberFormat
objects (lines 17-20) are used to format currency values and
percentages, respectively. Method
getCurrencyInstance
returns a
NumberFormat
object
that formats values as currency using the default locale for the system on which the app is
running. Similarly, method
getPercentInstance
returns a
NumberFormat
object that for-
mats values as percentages using the system's default locale. The
BigDecimal
object
tip-
Percentage
(line 22) stores the current tip percentage and is used in the tip calculation
(Fig. 25.20) when the user clicks the app's
Calculate
Button
.
14
public class
TipCalculatorController
15
{
16
// formatters for currency and percentages
17
private static final
NumberFormat currency =
18
NumberFormat.getCurrencyInstance();
19
private static final
NumberFormat percent =
20
NumberFormat.getPercentInstance();
21
22
private
BigDecimal tipPercentage =
new
BigDecimal(
0.15
);
// 15% default
23
24
// GUI controls defined in FXML and used by the controller's code
25
@FXML
26
private
TextField amountTextField;
27
28
@FXML
29
private
Label tipPercentageLabel;
30
31
@FXML
32
private
Slider tipPercentageSlider;
33
Fig. 25.19
|
TipCalculatorController
's
static
variables and instance variables. (Part 1 of 2.)