Java Reference
In-Depth Information
How it works...
Similar to other types, a string can be declared with a literal representation, participate in
expressions, and hold a value. The previous snippet shows the literal declaration of a string.
Variable str2 is coerced by the type-inference engine into a String type implicitly.
One of the interesting features of the String type in JavaFX is its ability to have embedded
expressions (similar to other templating languages) enclosed in curly braces. In the previous
code snippet, println ("The {title} has arrived!") will print the string with the
value of the variable title embedded in it.
You can also have complex expressions embedded in the string, as is shown from the code
snippet println("What are the odds {for(d in evens) "{d + 1} "}") from
the recipe. The embedded expression contains a loop that traverses elements from variable
evens and outputs the result from the nested string "{d + 1} " with each pass, producing
new string What are the odds 1 3 5 7 9 .
The JavaFX Sting type has the ability to process string formatting expressions based on
Java's java.util.Formatter class. In the previous code snippet, we used format
expression %,.2f to format the variable amount which displays Your house is
worth $445,234.66 . You can find information about supported format expressions at .
There is more...
Before we leave the discussion on String, it's worth taking a look at localization. In JavaFX,
the localization mechanism is an extension of the string expression.
Using JavaFX localization
To mark a String literal as a localized string, simply prefix the string with double hashes.
This causes JavaFX to substitute the string with a localized string, if one is found, from a
locale properties file.
To illustrate, let's look at an example. The code for this example is found in package
ch01/source-code/src/locale .
Create a JavaFX script file with the following content:
var msg1 = ## "Lift the cover";
var msg2 = ##[red button] "Press the red button to destroy";
println (msg1);
println (msg2);
Search WWH ::

Custom Search