This chapter provides an overview of the JavaFX™ Script programming language. It describes -- at a high level -- its main features, saving detailed coverage of specific constructs for subsequent chapters. This book is intended for designers and developers of rich Internet client applications and elements, which run in web pages, as Java™ Web Start software, or as traditional desktop applications. It assumes the reader to be familiar with JavaScript and/or the Java™ programming language. While this book does not define a formal language specification, it can be considered a complete reference for all currently supported language features.
The JavaFX Script programming language:
The following sections present a whirlwind tour of the JavaFX Script programming language. They provide a general introduction to its core syntax and capabilities, comparing and contrasting to the Java programming language where appropriate. Each topic is then covered in greater detail in subsequent chapters.
Class definitions share many similarities with the Java programming language, but there are differences as well.
State, for example, is information stored in attributes, not variables. Behavior is exposed through functions, not methods. The following
example defines a simple Rectangle
class that demonstrates the basic syntax of each.
class Rectangle { attribute width: Integer; attribute height: Integer; function grow(): Void { width++; height++; } function grow(amount: Integer): Void { width += amount; height += amount; } }
Unlike the Java programming language, class definitions are not required for scripts that simply use existing libraries. Additionally, the JavaFX Script programming language supports multiple inheritance, making it possible to inherit from more than one class if desired.
Classes are covered in detail in Chapter 2, Classes.
Attributes and Functions are covered in detail in Chapter 3, Attributes and Functions.
Object literals provide a simple syntax for class instantiation.
The following code creates a single instance of the Rectangle
class defined above, initializing its
width
and height
attributes to 100
(note that new
is not needed.)
Rectangle { width: 100 height 100 }
To store a reference to this object, use the var
keyword:
var myRect = Rectangle { width: 100 height 100 }
Objects are covered in detail in Chapter 2, Classes and Objects
Variables and basic data types are covered in detail in Chapter 4, Variables and Basic Data Types.
All programming languages have support for expressions, statements, and operators; the JavaFX Script programming language is no different. However, unlike the Java programming language, the JavaFX Script programming language is an expression language, which means that all executable statements -- inluding conditionals, loops, and even blocks -- are expressions.
Chapter 5, Expressions, Statements and Operators, discusses the expressions, statements, and operators available in the JavaFX Script programming language.
A sequence holds an ordered list of objects. This is roughly analogous to Java programming language arrays; both hold multiple values and are accessed by index starting at 0.
var week = ["Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday","Sunday"]; var mon = week[0]; var wed = week[2]; var fri = week[4];
Sequence slices are also supported:
var week = ["Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday","Sunday"]; var weekdays = week[0..4]; // first slice var weekend = week[5..6]; // second slice
Chapter 6 Sequences covers the basics of declaring sequences, while Chapter 7, Sequence Comprehensions, focuses on the supported sequence comprehension specifiers.
Data binding provides a simple syntax for synchronizing the state of multiple objects. When two objects are bound to one another, the second object's value automatically changes whenever the first object is updated. A common use of data binding is to keep GUI components synchronized with their underlying data.
import javafx.ui.Frame; import javafx.ui.Button; var myString = "Click Me"; Frame { width: 50 height: 50 visible: true content: Button { text: bind myString } } // If some other part of code changes myString // then the Button's text will automatically change // as well.
Data Binding is covered in detail in Chapter 8, Data Binding
Triggers are blocks of code that run when certain conditions are true. For example, you may want to take action to guard against an attribute's value being set to something that is inappropriate. The following example shows the basic trigger syntax:
import java.lang.System; ReplaceDemo { mySensitiveData: "Will anyone notice?" } class ReplaceDemo { attribute mySensitiveData: String on replace/programlisting> { System.out.println("I noticed a change!"); }; // application-specific safeguarding code would go here }
Triggers are covered in detail in Chapter 9, Triggers.