As I mentioned in the JFX and the Way Forward After JavaOne 2008 post, Sun announced at JavaOne that a preview release of the JavaFX SDK is scheduled to be available in July 2008. As a pleasant surprise over Memorial Day weekend, Sun opened up the development of this preview release SDK. This development activity is occurring as a part of the OpenJFX Compiler project, so follow the instructions that I gave you in the Obtaining the OpenJFX Script Compiler Just Got Easier post and join the fun! You'll be playing with the JavaFX SDK as it is being built, so expect changes. It would also be great if you'd provide input to the process, and help test the SDK as it's being developed.
Write Your First JavaFX Program that Uses the New Classes
The JavaFX code below uses the newer UI classes, and I'll show you how to compile and run this code in a bit. When the application first starts up, an empty window appears with two buttons:
When you click the Hello button, the message "You say hello..." from the popular "Hello, Goodbye" Beatles song displays approximately in the center of the window:
When you click the Goodbye button, the message "and I say goodbye" appears in place of the former message:
Here's the JavaFX code that generated this user interface and functionality:
/*
* HelloGoodbye.fx -
* A "Hello World" style program that demonstrates
* declaratively expressing a user interface.
*/
package beatles;
import javafx.ext.swing.BorderPanel;
import javafx.ext.swing.Button;
import javafx.ext.swing.Canvas;
import javafx.ext.swing.FlowPanel;
import javafx.ext.swing.Frame;
import javafx.scene.Font;
import javafx.scene.text.Text;
Frame {
var phrase:String
title: "Hello, Goodbye"
height: 300
width: 400
visible: true
content:
BorderPanel {
center:
Canvas {
content:
Text {
x: 50
y: 125
content: bind phrase
font:
Font {
size: 36
}
}
}
bottom:
FlowPanel {
content: [
Button {
text: "Hello"
action:
function():Void {
// The button was clicked
phrase = "You say hello...";
}
},
Button {
text: "Goodbye"
action:
function():Void {
phrase = "and I say goodbye";
}
}
]
}
}
}
Compiling and Running the Program
To compile this program, enter the following into the command line:
javafxc -d . HelloGoodbye.fx
As in Java, the -d option causes the CLASS files to be put into a directory corresponding to the package statement subordinate to the specified directory. To run the program, use the following command:
javafx beatles.HelloGoodbye
Now that you've got access to the JavaFX SDK as it's being built, get involved by writing JavaFX programs that exercise its functionality, and subscribe to one or more of the following mailing lists from this page.
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
Thanks for the lightning-fast response!
Posted by: Dave | June 27, 2008 at 04:07 PM
Yes, the Eclipse plug-in is not up to date. NetBeans is, and of course you can use the command-line tools.
Posted by: Jim Weaver | June 27, 2008 at 04:06 PM
Using the JavaFX plugin for Eclipse, I see this error:
11: Encountered "title" at line 11, column 3.
Was expecting one of:
"*" ...
"?" ...
"+" ...
"=" ...
This error points to the word 'title' in the first couple of lines of the program:
var phrase:String
title: "Hello, Goodbye"
Is the compiler for the Eclipse plugin not up to date?
Posted by: Dave | June 27, 2008 at 03:59 PM