Back in October I walked through a Hello World style program that binds the content attribute of a Text graphical object to a model. Today I'd like to show you the same program converted to compiled JavaFX Script syntax. Here's a screenshot of the program's UI, followed by the compiled JavaFX Script code. You may recall that it has a package statement, so I'll walk you through the process of compiling and running such a program.
/*
* HelloJFXBind.fx - A Compiled JavaFX Script "Hello World" style example
* binding to a model
*
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*/
package jfx_book;
import javafx.ui.*;
import javafx.ui.canvas.*;
/**
* This class serves as the model behind the user interface
*/
class HelloJFXModel {
attribute greeting:String;
}
/**
* This is a JavaFX Script that binds to data from the model.
*/
var hellojfxModel =
HelloJFXModel {
greeting: "Hello JavaFX Script Developer!"
};
Frame {
title: "JavaFX Script example that binds to a model"
background: Color.WHITE
height: 100
width: 400
content:
Canvas {
content: [
Text {
font:
Font {
faceName: "Sans Serif"
// Example of an attribute with a collection of values
style: [
FontStyle.BOLD,
FontStyle.ITALIC]
size: 24
}
// Put some color into the app
stroke: Color.RED
fill: Color.RED
x: 10
y: 10
content: bind hellojfxModel.greeting
}
]
}
visible: true
}
Compiling and Running this Example
Because this program has a package statement, the source code must be located in a directory with this same name as the package. To compile the program, set your current directory to where the source code is located and execute the javafxc command script, entering the following command from the jfx_book directory:
javafxc HelloJFXBind.fx
To run the program, go up one directory (to where the base of the package is), and enter the following command:
javafx jfx_book.HelloJFXBind
See the Obtaining the OpenJFX Script Compiler post to get the latest build of the compiler, and The Elephant is Through the Door post (no pun intended) to review the applicable differences between interpreted JavaFX Script and compiled JavaFX Script.
Enjoy,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site
Comments