Java Reference
In-Depth Information
You'll begin by test-driving the app, using it to calculate 15% and 10% tips. Then
we'll overview the technologies you'll use to create the app. You'll build the app's GUI
using the NetBeans and JavaFX SceneBuilder. Finally, we'll present the complete Java
code for the app and do a detailed code walkthrough.
Opening and Running the App
Open the NetBeans IDE, then perform the following steps to open the
Tip
Calculator
app's
project and run it:
1.
Opening the
Tip Calculator
app's project.
Select
File > Open Project…
or click the
Open Project…
( ) button on the toolbar to display the
Open Project
dialog.
Navigate to this chapter's examples folder, select
TipCalculator
and click the
Open Project
button. A
TipCalculator
node now appears in the
Projects
window.
2.
Running the
Tip Calculator
app.
Right click the
TipCalculator
project in the
Projects
window, then click the
Run Project
(
) button on the toolbar.
Entering a Bill Total
Using your keyboard, enter
34.56
, then press the
Calculate
Button
. The
Tip
and
Total
TextField
s show the tip amount and the total bill for a 15% tip (Fig. 25.9(b)).
Selecting a Custom Tip Percentage
Use the
Slider
to specify a
custom
tip percentage. Drag the
Slider
's
thumb
until the per-
centage reads
20%
(Fig. 25.9(c)), then press the
Calculate
Button
to display the updated
tip and total. As you drag the thumb, the tip percentage in the
Label
to the
Slider
's left
updates continuously. By default, the
Slider
allows you to select values from 0.0 to 100.0,
but in this app we'll restrict the
Slider
to selecting whole numbers from 0 to 30.
This section introduces the technologies you'll use to build the
Tip Calculator
app.
Class
Application
The main class in a JavaFX app is a subclass of
Application
(package
javafx.applica-
tion.Application
). The app's
main
method calls class
Application
's
static
launch
method to begin executing a JavaFX app. This method, in turn, causes the JavaFX runtime
to create an object of the
Application
subclass and call its
start
method, which creates
the GUI, attaches it to a
Scene
and places it on the
Stage
that method
start
receives as
an argument.
Arranging JavaFX Components with a
GridPane
Recall that layout containers arrange JavaFX components in a
Scene
. A
GridPane
(package
javafx.scene.layout
) arranges JavaFX components into
columns
and
rows
in a rectangu-
lar grid.
This app uses a
GridPane
(Fig. 25.10) to arrange views into two columns and five
rows. Each cell in a
GridPane
can be empty or can hold one or more JavaFX components,
including layout containers that arrange other controls. Each component in a
GridPane
can span
multiple
columns or rows, though we did not use that capability in this GUI.