RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


A Veteran JFC/Swing Developer Makes the Leap to JavaFX Script : Page 2

Find out why using JavaFX Script to build GUIs and add rich graphics functionality to Java applications is a no-brainer for seasoned Java Foundation Classes/Swing developers.

Functions Versus Operations
JavaFX Script offers both function and operation keywords. Functions usually are simple statements without complicated logic, while operations could have complex logic with exception handling. Use operations for writing complex APIs and functions for simple arithmetic functions.

Arrays in JavaFX Script have many enhanced features compared with arrays in other languages. The accompanying source code contains a working example (Array.fx) to demonstrate the array features such as INSERT, DELETE, and SELECT provided by JavaFX Script.

The syntax and semantics of JavaFX Script arrays are so close to human-readable text that you can almost read them like an instruction manual:

var myarr = [11,12,13]
Insert 10 as first into myarr;
Insert 14 as last into myarr;
Insert 9 before myarr[0];
Insert 15 after myarr[. ==14]

You can obtain implicit access to context objects using a dot operator as follows:

var nums = [1,2,3,4,11,13,14];
var valsGreaterThanEleven = nums[. > 11]; 
System.out.println("valsGreaterThanEleven = {valsGreaterThanEleven}"); 

The dot in the above code corresponds to the array object nums. The built-in functions do the comparison and return all values greater than 11.

The most intriguing feature of JavaFX Script arrays, however, is the ability to query them using select statements. Using a SELECT query to retrieve data from an array is similar to retrieving data from a relational database:

var ans2 = select n from n in [1,2,3,4,5] where n % 2 ==0;
//the above snippet prints all the even numbers from the array. 

See Listing 1. Array.fx for all the code related to JavaFX Script arrays. The code is easy to read and understand.

New Operators in JavaFX Script
Along with standard arithmetic and relational operators, JavaFX Script provides two new operators: format as and bind. The format as operator's function is pretty straightforward:

var v_int =42545;
var v_format= v_int.intValue() format as <<'$'###,###.##>>;
System.out.println("v_format={v_format}"); //displays $42,545

The Bind operator lets you bind two variable values. For example, you could bind the text of a label to a variable so that anytime the value of the variable changes, the change is reflected in the text of the label as well. Think of the bind functionality like plugging formulas into spreadsheet cells. The cells display values that are based on a formula.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date