As I mentioned earlier in the Book Example Updated for JavaFX Script SDK: HelloJFX post, several of the upcoming blog posts are going to contain the examples from my JavaFX Script book, rewritten to work with the JavaFX SDK Technology Preview Release. For your convenience, I'm placing these posts in the JavaFX Script Book Examples category. Today's post contains the HelloJFXBind2 program, which builds upon the HelloJFXBind program to demonstrate putting a model in its own file. Here is a screenshot of the UI:
By the way, the eBook version of the JavaFX Script book is available for download from this Apress site.
The Updated Code for HelloJFXBind2
This example program is comprised of two files. Here is the code, updated by a colleague and JavaFX developer named Matt Shirey:
HelloJFXBind2.fx
/*
* HelloJFXBind2.fx - A JavaFX Script "Hello World" style example
* binding to a model that is located in its own file.
*
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*
* Updated July 2008 by Matt Shirey ([email protected])
* for JavaFX SDK Technology Preview 1
*/
package jfx_book;
import javafx.ext.swing.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
/**
* This is a JavaFX Script that binds to data from the model.
*/
Frame {
var hellojfxModel =
HelloJFXModel {
greeting: "Howdy JavaFX Script Developer!"
}
title: "JavaFX Script example that binds to a model"
height: 100
width: 400
content:
Canvas {
content:
Text {
font:
Font {
name: "Sans Serif"
style: FontStyle.BOLD_ITALIC
size: 24
}
textOrigin: TextOrigin.TOP
// Put some color into the app
stroke: Color.RED
fill: Color.RED
x: 10
y: 10
content: bind hellojfxModel.greeting
}
}
visible: true
}
HelloJFXModel.fx
/*
* HelloJFXModel.fx - The model behind a JavaFX Script "Hello World" style
* example of binding.
*
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*
* Updated July 2008 by Matt Shirey (matt.shirey at lat-inc.com)
* for JavaFX SDK Technology Preview 1
*/
package jfx_book;
/**
* This class serves as the model behind the user interface
*/
class HelloJFXModel {
attribute greeting:String;
}
Running this Example
The JavaFX SDK Technology Preview branch of the compiler build may be downloaded here. This branch is what will become the JavaFX SDK Preview Release. After adding the openjfx-compiler-tp1/dist/bin directory to your PATH environment variable, and verifying that you have the Java Runtime Environment (JRE) 6 installed, use the following command at your operating system prompt to compile the program:
javafxc -d . *.fx
To run the program, use the following command:
javafx jfx_book.HelloJFXBind2
Have fun, and please post a comment if you have any questions!
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