JavaFX is a product family of Java technology introduced by Sun. JavaFX Script can be used to efficiently create rich media and highly interactive applications. JavaFX is a powerful competitor for Adobe Flex and Microsoft Silverlight. This paper introduces some advanced features of JavaFX script language itself and discusses some advanced topics in JavaFX script, including creating complex user interface, animating, JavaFX script application deployment and in a separate Java Embedded JavaFX Script in the application.
Introduction to JavaFX Script language
JavaFX is a Java-technology-based product family designed to provide a consistent user experience from desktops, mobile devices, set-top boxes to Blu-ray discs. JavaFX currently contains JavaFX Script and JavaFX Mobile. JavaFX Script can be used to efficiently create rich media and highly interactive applications. JavaFX is a powerful competitor for Adobe Flex and Microsoft Silverlight. This article introduces some advanced features of the JavaFX Script language itself through a concrete example. These features include block expressions, range expressions, sequence modifications, triggers (Trigger), and data binding. In addition, some advanced topics in JavaFX script are discussed in combination with specific applications, including creating complex user interfaces, implementing animations, JavaFX script applications, and embedding JavaFX script in a stand-alone Java application.
The JavaFX script language itself is constantly changing, and the descriptions and instances of JavaFX script syntax in this article are based on the version of JavaFX script in July 21, 2008. The development environment uses NetBeans 6.1 and installs the JavaFX Script plugin. Please download the relevant tools from the reference resources.
JavaFX Script Advanced Features
The following is a detailed description of some of the advanced features of the JavaFX Script language.
Block expression
The block expression in JavaFX Script is a series of semicolon-delimited statements contained in {}. If the last statement in the block expression is an expression, then the value of the block expression is the value of the last expression, otherwise the value of the block expression is a Void type. Block expressions are appropriate for computing logic that occurs only once in code. Because it only occurs once, you don't have to encapsulate such logic in a function. Block expressions can also distinguish this part of the logic from other parts of the code. In code Listing 1, the calculation logic for the salary is encapsulated in a block expression.
Listing 1. Block Expression Example
var baseSalary = 10000;
var salary = {
12 * (baseSalary + 1000) + 2 * baseSalary
};
System.out.println(salary);
Range expression
People who are familiar with other dynamic languages, such as Python, Ruby, and Groovy, may not be unfamiliar with range expressions. JavaFX Script also introduces the same range expression, which can be used to define a sequence. But the way it is used is somewhat different from other languages.
In JavaFX Script you can pass the [number1 ... NUMBER2] to define a sequence. Note here that the two boundary numbers number1 and number2 are included in the sequence. such as [0..5] contains 0,1,2,3,4,5 these six digits. This is the number1...number2 in Ruby and the Number1 in Groovy. Number2 is the same. By default, the interval between numbers in a range is 1, and you can declare the interval by adding an explicit step after the number2. For example [0..9 Step 3] contains a number that is 0,3,6,9.
When Number1 is greater than number2, you can generate a descending sequence by specifying a step with a negative value. If [5..0 step-1] contains a number that is 5,4,3,2,1,0, the resulting sequence is actually empty if you do not specify a step, or if the value of step is positive. such as [5..0] is an empty sequence.
Changes to the sequence
The JavaFX Script provides two powerful operator inserts and deletes to manipulate the sequence.
The syntax for the INSERT statement is shown in Listing 2.
Listing 2. Syntax for INSERT statements
insert x into seq
insert x before seq[idx]
insert x after seq[idx]
As you can see from the syntax listed in Listing 2, INSERT statements can insert new elements into a sequence at a specified location. Using insert X into SEQ adds x to the end of the sequence. While insert x before SEQ[IDX] and insert X after SEQ[IDX], you can insert new elements in front and back of the corresponding elements of SEQ[IDX respectively.