Adverts
The original HelloWorld tutorial page for JavaFX provided you with a first taste of the JavaFX Script language. Now it is time to do something a little more interesting with the language but first it is necessary to describe a little about the language itself. JavaFX uses a declarative syntax to express the relationship between various page components. This is in contrast to standard Java syntax which is procedural (at least when developing Swing code). The term "declarative language" is a little fuzzy and controversial. The only true declarative languages tend to be interface layout languages and many probably aren't actually programming languages at all as they lack general data processing capabilities. Reality however is not so clean cut and many languages have aspects of both declarative and imperative syntax. To be described as declarative a language has to specify what to do rather than how to do it - the how is left up to the interpreter.
A common example is SQL which specifies what data you want returned rather than how to get the data. In the case of JavaFX you specify that you want the contents of a text field to be linked to a label, for example, not how to make that link. This linking is carried out with the bind key word which is used to bind two or more fields together. One of the key aspects of a declarative language is that it has a root from which all other parts of the program stem. For example with a JavaFX application you will often have a Frame at the root which will then enclose the items that will be contained within the frame which will in turn contain the items they contain. This produces a tree that describes the whole interface in a clear and concise manner that is easy to understand. Contrast this with the procedural way of writing a Swing application where items are added to their parent manual and it quickly becomes clear that declarative syntax is a good way of producing interfaces.
The net program shows the first use of variables:
import javafx.ui.*;
var title = "Hello World Extended";
var win = Frame {
title: "JavaFX - {title}"
width: 300
height: 50
content: Label {
text: "Hello World"
}
visible: true
};
The title variable is used in the frame title. If you change the title contents and re-run the application you will see the frames title changes. While not the most exciting of example it does show how simple it is to set parameters. Imagine the equivalent Java code to do this. The next step is to start binding variables which I will discuss in the next tutorial Binding Variables .