Java Reference
In-Depth Information
Section 25.5.2 Technologies Overview
• A JavaFX app's main class inherits from
Application
(package
javafx.application.Application
).
•The main class'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
Appli-
cation
subclass and call its
start
method, which creates the GUI, attaches it to a
Scene
and plac-
es it on the
Stage
that method start recevies as an argument.
•A
GridPane
(package
javafx.scene.layout
) arranges JavaFX nodes into columns and rows in a
rectangular grid.
• 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.
•A
TextField
(package
javafx.scene.control
) can accept text input or display text.
•A
Slider
(package
javafx.scene.control
) represents a value in the range 0.0-100.0 by default
and allows the user to select a number in that range by moving the
Slider
's thumb.
•A
Button
(package
javafx.scene.control
) allows the user to initiate an action.
• Class
NumberFormat
(package
java.text
) can format locale-specific currency and percentage strings.
• GUIs are event driven. When the user interacts with a GUI component, the interaction—known
as an event—drives the program to perform a task.
• The code that performs a task in response to an event is called an event handler.
• For certain events you can link a control to its event-handling method by using the
Code
section
of Scene Builder's
Inspector
window. In this case, the class that implements the event-listener in-
terface will be created for you and will call the method you specify.
• For events that occur when the value of a control's property changes, you must create the event
handler entirely in code.
• You implement the
ChangeListener
interface (package
javafx.beans.value
) to respond to the
user moving the
Slider
's thumb.
• JavaFX applications in which the GUI is implemented as FXML adhere to the Model-View-
Controller (MVC) design pattern, which separates an app's data (contained in the model) from
the app's GUI (the view) and the app's processing logic (the controller). The controller imple-
ments logic for processing user inputs. The view presents the data stored in the model. When a
user provides input, the controller modifies the model with the given input. When the model
changes, the controller updates the view to present the changed data. In a simple app, the model
and controller are often combined into a single class.
• In a JavaFX FXML app, you define the app's event handlers in a controller class. The controller
class defines instance variables for interacting with controls programmatically, as well as event-
handling methods.
• Class
FXMLLoader
's
static
method
load
uses the FXML file that represents the app's GUI to cre-
ates the GUI's scene graph and returns a
Parent
(package
javafx.scene
) reference to the scene
graph's root node. It also initializes the controller's instance variables, and creates and registers
the event handlers for any events specified in the FXML.
Section 25.5.3 Building the App's GUI
• If a control or layout will be manipulated programmatically in the controller class, you must pro-
vide a name for that control or layout. Each object's name is specified via its
fx:id
property. You
can set this property's value by selecting a component in your scene, then expanding the
Inspector
window's
Code
section—the
fx:id
property appears at the top.