Java Reference
In-Depth Information
This chapter assumes that you have an understanding of GUI basics, including event
handling, as introduced in Chapters 15 and 16 .
JavaFX Basic Concepts
Before you can create a JavaFX application, there are several key concepts and features
you must understand. Although JavaFX has similarities with Java's other GUIs, the AWT
and Swing, it has substantial differences. For example, like Swing, JavaFX components
are lightweight and events are handled in an easy-to-manage, straightforward manner.
However, the overall organization of JavaFX and the relationship of its main components
differ significantly from either Swing or the AWT. Therefore, a careful reading of the fol-
lowing sections is recommended.
The JavaFX Packages
The JavaFX framework is contained in packages that begin with the javafx prefix. At the
time of this writing, there are more than 30 JavaFX packages in its API library. Here are
four examples: javafx.application , javafx.stage , javafx.scene , and javafx.scene.layout .
Although we will only use a few JavaFX packages in this chapter, you will want to spend
some time browsing their capabilities. JavaFX offers a wide array of functionality.
The Stage and Scene Classes
The central metaphor implemented by JavaFX is the stage . As in the case of an actual stage
play, a stage contains a scene . Thus, loosely speaking, a stage defines a space and a scene
defines what goes in that space. Or, put another way, a stage is a container for scenes and a
scene is a container for the items that comprise the scene. As a result, all JavaFX applica-
tions have at least one stage and one scene. These elements are encapsulated in the JavaFX
API by the Stage and Scene classes. To create a JavaFX application, you will, at minimum,
add at least one Scene object to a Stage . Let's look a bit more closely at these two classes.
Stage is a top-level container. All JavaFX applications automatically have access to one
Stage , called the primary stage . The primary stage is supplied by the run-time system when
a JavaFX application is started. Although you can create other stages, for many applica-
tions, the primary stage will be the only one required.
As mentioned, Scene is a container for the items that comprise the scene. These can con-
sist of controls, such as push buttons and check boxes, text, and graphics. To create a scene,
you will add those elements to an instance of Scene .
Nodes and Scene Graphs
Search WWH ::




Custom Search