Java Reference
In-Depth Information
To set the fill property in the OnTheScene example, instead of using one of the constants in the
Color
class
(e.g.,
Color.BLUE
), we're using an RGB formula to create the color. Take a look at the
javafx.scene.paint.Color
class
in the JavaFX API docs and scroll down past the constants such as
ALICEBLUE
and
WHITESMOKE
to see the constructors
and methods. We're using a constructor of the
Color
class, setting the fill property to it, as shown in the following
snippet from Listing 2-2.
sceneRef.setFill(new Color(fillValue, fillValue, fillValue, 1.0));
As you move the Slider, to which the
fillVals
property is bound, each of the arguments to the
Color()
constructor is set to a value from 0 to 255, as indicated in the following code snippet from Listing 2-2.
fillVals.bind(sliderRef.valueProperty());
Populating the Scene with Nodes
As covered in Chapter 1, you can populate a
Scene
with nodes by instantiating them and adding them to container
nodes (e.g.,
Group
and
VBox
) that can contain other nodes. These capabilities enable you to construct complex
scene
graphs
containing nodes. In the example here, the root property of the
Scene
contains a
Flow
layout container, which
causes its contents to flow either vertically or horizontally, wrapping as necessary. The
Flow
container in our example
contains an
HBox
(which contains a
Slider
and a
ChoiceBox
) and several other nodes (instances of
Text
,
Hyperlink
,
and
RadioButton
classes).
Finding a Scene Node by ID
Each node in a
Scene
can be assigned an ID in the
id
property of the node. For example, in the following snippet from
Listing 2-2, the
id
property of a
Text
node is assigned the
String "sceneHeightText"
. When the action event handler
in the Hyperlink control is called, the
lookup()
method of the
Scene
instance is used to obtain a reference to the node
with the
id
of
"sceneHeightText"
. The event handler then prints the content of the
Text
node to the console.
■
the hyperlink control is essentially a button that has the appearance of hyperlink text. it has an action event
handler in which you could place code that opens a browser page or any other desired functionality.
Note
textSceneH = new Text();
textSceneH.getStyleClass().add("emphasized-text");
textSceneH.setId("sceneHeightText");
Hyperlink hyperlink = new Hyperlink("lookup");
hyperlink.setOnAction((javafx.event.ActionEvent e) -> {
System.out.println("sceneRef:" + sceneRef);
Text textRef = (Text) sceneRef.lookup("#sceneHeightText");
System.out.println(textRef.getText());
});
A close examination of the action event handler reveals that the
lookup()
method returns a
Node
, but the actual
type of object returned in this snippet is a
Text
object. Because we need to access a property of the
Text
class (text)
that isn't in the
Node
class, it is necessary to coerce the compiler into trusting that at runtime the object will be an
instance of the
Text
class.
Search WWH ::
Custom Search