Java Reference
In-Depth Information
flection values range from zero (0%) to one (100%). The following code snippet imple-
ments a reflection of 80% with a float value of 0.8f:
Reflection refl = new Reflection();
refl.setFraction(0.8f);
java8Recipes5.setEffect(refl);
Solution 2 demonstrates how to construct a user interface using FXML, CSS, and
Java. While this recipe focuses on text and fonts, it is important to note that FXML
solutions clearly follow a model-view-controller standard, separating UI code from
business logic. It is also important to note that if the UI in this example were to contain
buttons or other nodes that contained actions, a controller class would need to be cre-
ated as well to embody the action logic.
In the second example, an FXML file contains the structured layout for the user in-
terface, which consists of a
Scene
,
Pane
,
TextFlow
, and a series of
Text
nodes.
The scene contains a
<stylesheets>
element, which is used to specify which style
sheets to apply to the elements within the XML. The
Pane
node is used as a base for
the layout, and it contains each of the other nodes within the UI. The
TextFlow
node
is new in JavaFX 8, and it is a special layout that is designed to lay out rich text. The
TextFlow
can lay many different
Text
nodes into a single flow.
As you can see from the FXML, each of the
Text
nodes within the
TextFlow
have different styles associated with them, based on those styles that have been defined
within the attached style sheet. The properties for styles in JavaFX style sheets are pre-
ceded by
-fx-
, and property names and values are separated by a colon and termin-
ated by a semicolon (
;
). For the most part, JavaFX style properties align nicely with
standard CSS properties. For a complete summary, refer to the documentation at
ht-
The
TextFlow
uses the text and font of each node that is embedded within, as
well as its own width and text alignment, to determine the placement of the text. Nodes
other than
Text
can also be embedded within a
TextFlow
. When adding
Text
nodes to a
TextFlow
, you can set word wrap by specifying a maximum width of the
TextFlow
via the
setMaxWidth()
method. It is also possible to include a
\n
at
the end of any strings within a
Text
node to initiate a line break. The following code
performs the same solution as 1, but uses
TextFlow
to lay out the
Text
nodes, rather
than adding each to the scene graph separately.