In response to the The JavaFX SDK Packages are Taking Shape post, a reader named John writes: "I started with the examples in your nice book. However, I see that many things have changed in JFX since then. E.g. javafx.ui.* classes have been replaced by javafx.ext.swing.* classes? And of course many examples do not run. Will you create an updated source of the examples of the book that run with the latest JFX update?"
I responded in agreement, so the next several blog posts are going to contain the examples from my JavaFX Script book, rewritten to work with the JavaFX SDK Technology Preview Release. The first one is the HelloJFX program, for which this is 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 HelloJFX
Here is the code, updated by a colleague and JavaFX developer named Matt Shirey:
/*
* HelloJFX.fx - A JavaFX Script "Hello World" style example
*
* Developed 2007 by James L. Weaver (jim.weaver at jmentor 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.text.*;
Frame {
title: "Hello World-style example for JavaFX Script"
height: 100
width: 400
content:
Canvas {
content:
Text {
font:
Font {
name: "Sans Serif"
style: FontStyle.BOLD
size: 24
}
x: 10
y: 10
textOrigin: TextOrigin.TOP
content: "Hello JavaFX Script Developer!"
}
}
// Show the Frame on the screen
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 . HelloJFX.fx
To run the program, use the following command:
javafx jfx_book.HelloJFX
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
I don't know why the example above doesn't work for me:
-NetBeans IDE 6.1 (Build 200804211638)
-1.6.0_03; Java HotSpot(TM) Client VM 1.6.0_03-b05
What's wrong? Has something change since July?
I had to make your code more like this:
package jfx_book;
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.text.Text;
import javafx.scene.FontStyle;
import javafx.scene.Font;
Frame {
title: "Hello World-style example for JavaFX Script"
height: 100
width: 400
stage:
Stage {
content:
Text {
font:
Font {
name: "Sans Serif"
style: FontStyle.BOLD
size: 24
}
x: 15
y: 50
content: "Hello JavaFX Script Developer!"
}
}
// Show the Frame on the screen
visible: true
}
Posted by: Piotr Hełka | September 08, 2008 at 10:34 AM