Book Image

JavaFX 1.2 Application Development Cookbook

By : Vladimir Vivien
Book Image

JavaFX 1.2 Application Development Cookbook

By: Vladimir Vivien

Overview of this book

JavaFX Script enables you to easily create rich Internet applications by embedding multimedia components. Although you can create stylish Internet applications by modifying these default components, even advanced users find it challenging to create impressive feature-rich Internet applications with JavaFX. Also, there are limited JavaFX components to work with and by default these components don't look visually appealing.This book explores limitless possibilities to style your application by coding JavaFX components to display your content in a more appealing fashion. The recipes in this book will help you to create customized JavaFX components with which you can make modern, feature-rich applications.First, you will be introduced to the JavaFX SDK and other development tools available to help you be productive during development. You will create an application in JavaFX by arranging complex graphical components (and non-graphical libraries) with simplified declarative constructs. You will then explore the fun side of JavaFX by using transformation techniques to manipulate the location and dimensions of objects. The next chapter is about the GUI components that are available in the framework, which provide a high level of interactivity. You will learn how to use the media component to play media content. Then we will access data and manipulate data locally or remotely. You will explore many deployment options and integration tips and tricks to take advantage of runtime contexts. Finally, you will interact with pure Java code to read and write files in JavaFX and to establish interactions with computing platforms.
Table of Contents (18 chapters)
JavaFX 1.2 Application Development Cookbook
Credits
About the Author
About the Reviewers
Preface
Mobile JavaFX
JavaFX Composer
JavaFX Products and Frameworks
Best Practices for Development
Best Practices for Deployment

Working with JavaFX String


String is a fundamental value type in JavaFX. Similar to Java and other languages on the JVM, the String type is used to represent text literals within a single or double quotes. Unlike Java, however, JavaFX strings have additional capabilities which will be explored in this section.

Getting ready

You should be familiar with the notion of string literals and expressions.

How to do it...

We have already seen how to use String types in other recipes. When creating a String, you simply create a literal or expression to represent the string's content, and use the curly braces to embed expressions as shown below. The full listing for this code can be found in ch01/source-code/src/javafx/StringDemo.fx.

var str1:String = "Hello World!";
var str2 = "Goodbye forever";
var title = "King";
println ("The {title} has arrived!");
var evens = [0, 2, 4, 6, 8];
println("What are the odds {for(d in evens) "{d + 1} "}");
var amount = 445234.66;
println ("Your house is worth ${%,.2f amount}");

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 http://java.sun.com/javase/6/docs/api/java/util/Formatter.html.

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);

Save the file as Localization.fx and compile. Now, create a text file named Localization_fr.fxproperties and type in the following:

"Lift the cover"="Soulevez le couvercle"
"red button"="Appuyez sur le bouton rouge pour détruire"

Notice that JavaFX can use either the actual string or a string key (red button) to do the substitution for the localized string. When the code is compiled and executed, the output is:

Soulevez le couvercle
Appuyez sur le bouton rouge pour détruire

The strings are substituted automatically by the JavaFX runtime with their French translation. If no properties file is found for the locale, JavaFX defaults to the actual String assigned to the variable.