Content
Comparison declarative syntax with procedural syntax
Adding dynamic behavior to a program
Learn more about JavaFX GUI components
About translators
Comparing declarative and procedural syntax
As we have seen in the previous section, the JavaFX language provides a declarative syntax for expressing the contents of structures and user interface components. To help you understand, let's rewrite the above program in a purely procedural way, as we often do when writing swing programs:
var win = new Frame();
win.title = "Hello World JavaFX";
win.width = 200;
var label = new Label();
label.text = "Hello World";
win.content = label;
win.visible = true;
The source code above is also a valid JavaFX program, which has the same effect as the previous code.
The following is a list of what actually happens in declarative and procedural methods:
Call the Frame class construct method to create a new frame.
Assign values to the title, width, visible, and content properties of the frame.
In the process of assigning the content property, the constructor for the label class is invoked to create a new label and assign a value to its Text property.
Although the above code is a very simple example, it is easy to see from the first example and the example above that writing a program with declarative syntax makes the code more understandable.
Therefore, declarative programming uses simple expressions to build an application. In the first example above, the root (root) of an expression is often an object assignment expression (constructor) that generates the object graph that makes up the program.