Jim has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

Binding the User Interface to the Model in JavaFX

07.08.2008
| 6383 views |
  • submit to reddit

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 HelloJFXBind program, which builds upon the HelloJFX program to demonstrate binding the UI to a model. Here is a screenshot of the UI:

Hellojfxbind

By the way, the eBook version of the JavaFX Script book is available for download from this Apress site.

The Updated Code for HelloJFXBind

Here is the code, updated by a colleague and JavaFX developer named Matt Shirey:

/*
* HelloJFXBind.fx - A JavaFX Script "Hello World" style example
* binding to a model
*
* 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;

import javafx.ext.swing.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;

/**
* 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"
height: 100
width: 400
content:
Canvas {
content:
Text {
font:
Font {
name: "Sans Serif"
// Example of an attribute with a collection of values
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
}

 

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 . HelloJFXBind.fx

To run the program, use the following command:

javafx jfx_book.HelloJFXBind

 

Published at DZone with permission of its author, Jim Weaver.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: