Java Reference
In-Depth Information
Creating the Scene
To display the GUI, you must attach it to a Scene , then attach the Scene to the Stage that's
passed into method start . Line 17 creates a Scene , passing root (the scene graph's root
node) as an argument to the constructor. By default, the Scene 's size is determined by the
size of the scene graph's root node. Overloaded versions of the Scene constructor allow you
to specify the Scene 's size and fill (a color, gradient or image), which appears in the Scene 's
background. Line 18 uses Scene method setTitle to specify the text that appears in the
Stage window's title bar. Line 19 places Stage method setScene to place the Scene onto
the Stage . Finally, line 20 calls Stage method show to display the Stage window.
25.5.5 TipCalculatorController Class
Figures 25.18-25.21 present the TipCalculatorController class that responds to user
interactions with the app's Button and Slider . Replace the code that NetBeans generated
in TipCalculatorController.java with the code in Figs. 25.18-25.21.
Class TipCalculatorController 's import Statements
Figure 25.18 shows class TipCalculatorController 's import statements.
1
// TipCalculatorController.jav
2
// Controller that handles calculateButton and tipPercentageSlider events
3
import java.math.BigDecimal;
4
import java.math.RoundingMode;
5
import java.text.NumberFormat;
6
import javafx.beans.value.ChangeListener;
7
import javafx.beans.value.ObservableValue;
8
import javafx.event.ActionEvent;
9
import javafx.fxml.FXML;
10
import javafx.scene.control.Label;
11
import javafx.scene.control.Slider;
12
import javafx.scene.control.TextField;
13
Fig. 25.18 | TipCalculatorController 's import declarations.
Lines 3-12 import the classes and interfaces used by class TipCalculatorController :
•Cl ss BigDecimal of package java.math (line 3) is used to perform precise mon-
etary calculations. The RoundingMode enum of package java.math (line 4) is used
to specify how BigDecimal values are rounded during calculations or when for-
matting floating-point numbers as String s.
•Cl ss NumberFormat of package java.text (line 5) provides numeric formatting
capabilities, such as locale-specific currency and percentage formats. For example,
in the U.S. locale, the monetary value 34.56 is formatted as $34.95 and the per-
centage 15 is formatted as 15%. Class NumberFormat determines the locale of the
system on which your app runs, then formats currency amounts and percentages
accordingly.
You implement interface ChangeListener of package javafx.beans.value (line
6) to respond to the user moving the Slider 's thumb. This interface's changed
 
 
Search WWH ::




Custom Search