Java Reference
In-Depth Information
container to affect certain results. Static properties are represented in FXML files as attributes on the inner object
(the object that is passed in as the first parameter to the static method) with a name that includes both the class name
and the static method name separated by a dot. You can spot an example of a static property in Listing 3-4:
<WebView fx:id="webView"
prefHeight="200.0"
prefWidth="200.0"
VBox.vgrow="ALWAYS"/>
Here we are adding a WebView to a VBox , and the VBox.vgrow attribute indicates that the FXMLLoader needs to call
VBox.vgrow(webView, Priority.ALWAYS)
prior to adding webView to the VBox . Static properties can also appear as subelements in addition to appearing as
attributes.
Understanding Attribute Resolutions and Bindings
As you have seen in earlier parts of this chapter, object properties can be represented both as attributes and
as subelements. Sometimes it is as effective to model a property as a subelement or an attribute. However, the
FXMLLoader will perform additional processing for attributes, making it more attractive to use attributes. When
processing attributes, the FXMLLoader will perform three kinds of attribute value resolutions and expression binding.
When an attribute's value starts with an “ @ ” character, FXMLLoader will treat the value as a location relative to the
current file. This is called location resolution . When an attribute's value starts with a “ % ” character, FXMLLoader will
treat the value as a key in a resource bundle and substitute the locale-specific value for the key. This is called resource
resolution . When an attribute's value starts with a “ $ ” character, FXMLLoader will treat the value as a variable name,
and substitute the value of the referenced variable as the value of the attribute. This is called variable resolution .
When an attribute's value starts with “ ${ ” and ends with “ } ”, and if the attribute represents a JavaFX property,
FXMLLoader will treat the value as a binding expression, and binds the JavaFX property to the enclosed expression.
This is called expression binding . You will learn about JavaFX properties and bindings in Chapter 4. For now simply
understand that when a property is bound to an expression, every time the expression changes value, the change is
reflected in the property. Supported expressions include string literals, boolean literals, numeric literals, the unary
- (minus) and ! (negation) operators, the arithmetic operators ( + , - , * , / , % ), the logical operators ( && , || ), and the
relational operators ( > , >= , < , <= , == , != ).
The ResolutionAndBindingExample, shown in Listings 3-14 to 3-19, illustrates the use of location resolution,
resource resolution, and variable resolution as well as expression binding.
Listing 3-14. ResolutionAndBindingExample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import java.util.Date?>
<VBox id="vbox" alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="200.0" prefWidth="700.0" spacing="10.0"
stylesheets="@ResolutionAndBindingExample.css" xmlns=" http://javafx.com/javafx/8 "
xmlns:fx=" http://javafx.com/fxml/1 " fx:controller="ResolutionAndBindingController">
 
Search WWH ::




Custom Search