Java Reference
In-Depth Information
you click an interactive control, it receives the focus. Similarly, when you press the Ta b
key, the focus transfers from the current focusable control to the next one—this occurs in
the order the controls were added to the GUI. Interactive controls—such as TextField s,
Slider s and Button s—are focusable by default. Non-interactive controls—like Label s—
are not focusable.
In this app, the tipTextField and totalTextField are neither editable nor focus-
able. To make these changes, select both TextField s, then in the Inspector 's Properties
section, uncheck the Editable and Focus Traversable properties.
Step 10: Setting the Slider 's Properties
To complete the GUI, you'll now configure the Tip Calculator 's Slider . By default, a
Slider 's range is 0.0 to 100.0 and its initial value is 0.0. This app allows tip percentages
from 0 to 30 with a default of 15. To make these changes, select the Slider , then in the
Inspector 's Properties section, set the Slider 's Max property to 30 and the Value property
to 15 . We also set the Block Increment property to 5 —this is the amount by which the Val-
ue property increases or decreases when the user clicks between an end of the Slider and
the Slider 's thumb. Save the FXML file by selecting File > Save .
Previewing the Final Layout
Select Preview > Show Preview in Window to view the final GUI (Fig. 25.16). When we
discuss the TipCalculatorController class in Section 25.5.5, we'll show how to specify
the Calculate Button 's event handler in the FXML file.
Fig. 25.16 | Final GUI design previewed in Scene Builder.
25.5.4 TipCalculator Class
When you created the Tip Calculator app's project, NetBeans created two Java source-code
files for you:
TipCalculator.java —This file contains the TipCalculator class, in which the
main method (discussed in this section) loads the FXML file to create the GUI
and attaches the GUI to a Scene that's displayed on the app's Stage .
TipCalculatorController.java —This file contains the TipCalculatorCon-
troller class (discussed in Section 25.5.5), which is where you'll specify the
Slider and Button controls' event handlers.
Figure 25.17 presents class TipCalculator . We reformatted the code to meet our conven-
tions. With the exception of line 18, all of the code in this class was generated by Net-
 
 
Search WWH ::




Custom Search