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

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.

ver since the Internet emerged, developers and the user community have tried tirelessly to lend Internet application interfaces the same interactivity their desktop applications offered. As the Internet evolved, client-side scripting languages such as JavaScript allowed developers to add dynamic behavior to browser-based applications. However, because different browsers handle JavaScript differently, creating complicated JavaScript code that works across all browsers has been difficult.

Sun Microsystems recently offered Java developers a solution to this challenge with a new scripting language called JavaFX Script. Applications built in JavaFX Script can run on the Internet, the desktop, or even on mobile devices. Unlike predecessors such as applets that tried to accomplish the same cross-platform functionality, JavaFX Script is built from the ground up with performance in mind. Its GUI components and back-end application objects are implemented in Java, and JavaFX Script code is used only to create such components, configure them, and wire them together. Also, because it's built around the Java language, JavaFX Script inherits great Java features for building large-scale reusable code such as packaging and importing.

For seasoned Java programmers who heretofore have relied on Java Foundation Classes (JFC)/Swing for building GUIs and adding rich graphics functionality to their Java applications, learning JavaFX Script is a no-brainer. This article introduces JavaFX Script language fundamentals, making comparisons to JFC wherever applicable.

Language Fundamentals
JavaFX Script provides two flavors of syntax: declarative and procedural. The declarative syntax is a simple list of human-readable commands, whereas the procedural syntax takes the form of simple programs with curly braces. Both syntaxes are interpreted at run time.

The following are some other notable JavaFX Script fundamentals:

  1. You must store all JavaFX Script files with the extension .fx.
  2. You can use System.out.println from Java to print debug statements to the screen.
  3. Statements inside curly braces ({ }) are executed, even if they are within double quotes ("").

JavaFX Script provides four primitive data types, which you declare using var: String, Boolean, Number, and Integer. Each data type is tied to a corresponding Java Foundation Class. Hence you can access all the methods provided in the corresponding Java class from JavaFX Script as follows:

var str:String="World" //all methods of java.lang.String //class are available for str
str.substring(1);  // yields "World";

JavaFX Script also introduces cardinality notation while declaring variables:

  • A question mark (?) corresponds to optional cardinality.
  • A plus sign (+) corresponds to one or more cardinality.
  • An asterisk (*) corresponds to zero or more cardinality.

Here are examples of ? and *:

var txt: String?; //txt takes any value or optionally        //null
var myArr:Number* = [1,2,3] // same as var myArr=[1,2,3]

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