| By James L. Weaver | Article Rating: |
|
| November 23, 2007 08:00 AM EST | Reads: |
13,456 |
From Jim Weaver's Learn JavaFX Weblog
There are several environments in which you can develop and run JavaFX Script programs. I’d like to help you become a JavaFX Script programmer in the next few minutes, so I’m going to show you the fastest route that I know of to get there, which includes using a tool that Sun created called JavaFXPad.
- You’ll need the Java Runtime Environment (JRE) 1.5 or higher (Mac OS requires the latest JRE 1.5 release or JRE 1.6)
- Run JavaFXPad straight from the Internet by accessing the following URL: http://download.java.net/general/openjfx/demos/javafxpad.jnlp. This will launch JavaFXPad via Java Web Start, which is a Java application deployment technology. Each time you access this URL it will check for the latest version of JavaFXPad, download it, and automatically execute it.
- Cut and paste the following code into JavaFXPad, replacing the code that is already there.
/*
* HelloJFX.fx - A JavaFX Script
"Hello World" style example
*
* Developed 2007 by James L. Weaver
(jim.weaver at jmentor dot com)
*/
package jfx_book;
import javafx.ui.*;
import javafx.ui.canvas.*;
Frame {
title: "Hello World-style example for
JavaFX Script"
height: 100
width: 400
content:
Canvas {
content:
Text {
font:
Font {
faceName: "Sans Serif"
style: BOLD
size: 24
}
x: 10
y: 10
content: "Hello JavaFX Script Developer!"
}
}
// Show the Frame on the screen
visible: true
}
// End of listingLet’s walk through the code:
Comments
There are two types of comments in JavaFX: multiline comments and single-line comments. Multiline comments begin with the two characters /* and end with the same two characters in reverse order (*/). JavaFX will ignore anything in between. The beginning of the listing shows an example of a multiline comment. Single-line comments begin with the two characters (//). Anything that follows these two characters on a single line will be ignored. An example of a single-line comment is shown near the bottom of the code listing.
The package Declaration
JavaFX packages are analogous to folders in a file system. They provide a way to logically organize the source code files that comprise an application. The package in the preceding example is jfx_book, which indicates that the HelloJFX.fx source code is located in a folder named jfx_book. Package names may consist of more than one node (e.g., com.jmentor.jfx_book), in which case the source code file would be located in a folder named jfx_book that is located in a folder named jmentor, and so on. In fact, it is customary for a package name to begin with the domain name of the company or organization that developed the application (in reverse order, beginning with the top-level domain name, such as com or org).
The package declaration is optional, but it is a very good practice to use it in all but the most trivial programs. If used, the package statement must be at the top of the source code (excluding whitespace and comments).
import Statements
JavaFX programs typically use libraries that consist of JavaFX (and optionally Java) code. In this example, each import statement indicates the location (package) of the JavaFX classes that the code in the rest of this HelloJFX.fx file depends on for outputting widgets and drawing to the screen. An import statement can end with an asterisk (*), indicating that the program may use any of the classes in the package. An alternative form is to specifically name each class being used, as in the following example:
import javafx.ui.Frame;
All but the most trivial applications should organize their source code via package declarations. A source code file uses import statements to indicate its use of classes contained in source code files that have a different package statement.
An import statement may appear anywhere in your JavaFX source code, and whenever one is encountered, the imported JavaFX file is run as deemed appropriate.
Declarative Code That Defines the User Interface
One of the most exciting features of JavaFX is its ability to express a graphical user interface (GUI) using a simple, consistent, and powerful declarative syntax. Declarative programming, as opposed to procedural programming, consists of a single expression (rather than multiple expressions that are executed sequentially). JavaFX supports both types of programming, but it is good practice to use declarative syntax whenever possible.
In this example, the entire program (excluding the package and import statements) is declarative, in that it consists of one expression. This declarative expression begins by defining a Frame object followed by an open curly brace, and ends with the matching curly brace in the last line of the program. Nested within that are attributes of the Frame object, including the content attribute, which is assigned a Canvas widget (GUI component). Nested within that is the content attribute of the Canvas widget, which is assigned a Text object, and so on.
Declarative code automatically creates an instance (also known as an object) of each JavaFX class in the expression. It also assigns values to the attributes of the new instance. For example, look at the portion of code that creates an instance of the Font class:
Font {
faceName: "Sans Serif"
style: BOLD
size: 24
}This code creates an instance of the JavaFX Font class, and assigns the value Sans Serif to the faceName attribute of the new Font instance. Notice that the attribute name is always followed by a colon (:), which in JavaFX declarative syntax means “assign the value of the expression on the right to the attribute on the left.” These same concepts are true for all of the classes (Frame, Canvas, and Text) in this script. Let’s look at each of these classes individually.
Using the Frame ClassA Frame represents a GUI window, which has its own border, and can contain other GUI components within it.
Note: In this trivial HelloJFX.fx example, as shown in the graphic above, JavaFXPad renders the Frame object as a rectangular area within the output area, as opposed to a separate window. In slightly less trivial JavaFX programs, JavaFXPad renders the Frame object as a separate window.
As with any class, the Frame class has a set of attributes. The set of attributes that Frame widgets have, as shown in the following code snippet from the listing, are as follows:
- A title that appears in the title bar of the window (again, not shown by JavaFXPad in this trivial program).
- A height and width (in pixels) that determine how high and wide the window will initially be.
- A content attribute that defines what the contents of the Frame object will be. In this case, the Frame object will contain a Canvas widget on which you’ll draw a Text object that contains the message to be displayed.
- A visible attribute (after the closing brace of the Canvas widget) that controls whether the Frame object is to be shown on the screen just yet.
Frame {
title: "Hello World-style example for
JavaFX Script"
height: 100 width: 400 content:
...some code omitted...
// Show the Frame on the screen
visible: true
}Creating String Literals
One of the data types that JavaFX has is the String, which consists of zero or more characters strung together. As shown in the following title attribute of the Frame object, a String literal is defined by enclosing a set of characters in double quotes:
title: "Hello World-style example
for JavaFX Script"Alternatively, String literals may be enclosed in single quotes.
Using the Canvas GUI Widget
The purpose of the Canvas widget is to draw two-dimensional (2D) graphics, including lines, shapes, and text. It is a JavaFX class, but I’m referring to it as a widget here because it is a subclass of the JavaFX Widget class. As shown following, the content attribute of the Canvas widget indicates what will be drawn on the canvas, in this case some text:
Canvas {
content: Text {
…some code omitted…
}
}Drawing Text
To draw some text on the canvas, you use the Text class, supplying as attributes the x and y location (in pixels) at which the upper left-hand corner of the text should appear. The content attribute of the Text class contains the string that will be drawn, and the font attribute specifies the appearance of the text that will be drawn.
Text {
font: Font {
faceName: “Sans Serif”
style: BOLD
size: 24
}
x: 10
y: 10
content: “Hello JavaFX Script Developer!”
}Defining Fonts
And finally, at the innermost level of the declarative script that defines the UI for this application, we find the Font class (see the preceding code snippet). This class is used to specify the characteristics of the Text widget using the faceName, style, and size attributes shown.
Summary
Hopefully, you've achieved the objective of creating your first JavaFX Script program. Feel free to play with the source code in the JavaFXPad window and watch the effects (for example, change the values assigned to the x, y, or content attributes of the Text object). Some future posts will build on the knowledge that you've gained in this one. This post by the way, is an excerpt from my recently published book entitled "JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
", which is published by Apress.
- A title that appears in the title bar of the window (again, not shown by JavaFXPad in this trivial program).
Published November 23, 2007 Reads 13,456
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
- Client-Side Java: Rolling Out JavaFX at JavaOne
- JavaOne - JavaFX abuzz
- JavaFX Comments on Comments
- AJAX, Flash, Silverlight, or JavaFX: Must We Choose?
- How To Develop and Run a JavaFX Script Program Using JavaFXPad
- Closures in Compiled JavaFX Script
- Developing Your First Compiled JavaFX Script Program
- Jim Weaver's JavaFX Puzzler: The Puzzler4Compiled Program
- Playing with UI Features in Compiled JavaFX Script
More Stories By James L. Weaver
James L. (Jim) Weaver is founder and president of jMentor, formed in 2000 to provide Java programming-related training to companies and individuals. He has served as a system architect and developer for over 25 years, specializing in leading-edge software development. His specialties include Java, object-oriented, and web-based technologies. He has authored books on the Java programming language, including most recently JavaFX Script, published by Apress.
![]() |
Jim Weaver 11/22/07 10:58:23 AM EST | |||
I'll be covering JavaFX Mobile in my [http://learnjavafx.typepad.com Learn JavaFX blog] as JavaFX Mobile materializes. Meanwhile, [http://www.sun.com/software/javafx/mobile/ Sun's JavaFX Mobile website] is a good place to begin understanding their vision Thanks, |
||||
![]() |
Jim Weaver 11/22/07 10:18:57 AM EST | |||
Puzzled, You are exactly right about JavaFX Script making Swing easier to use. Here's an excerpt from a [http://learnjavafx.typepad.com/weblog/2007/10/putting-my-cto-.html post from my Learning JavaFX blog] that addresses this, and some other JavaFX strengths: The following list describes some of the strengths of JavaFX Script: * Its simple, declarative syntax used to express user interfaces, including a very rich set of layout widgets that make easy work of laying out a user interface in a platform-independent way. Content developers can create great looking, functional user interfaces without being expert programmers. A side benefit is that it is enables fast development of application prototypes. Another side benefit is that it would be a great language to use in schools to teach programming concepts. Thanks, |
||||
![]() |
queZZtion 11/22/07 10:16:28 AM EST | |||
How about Java FX mobile? |
||||
![]() |
Puzzled 11/22/07 09:37:50 AM EST | |||
Why do we need the JavaFX Script? Isn't Swing good enough? Or does JFX Script make Swing easier to use? |
||||
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Journal Opens "Readers' Choice Awards" Nominations
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Industry Experts Discuss the State of Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- An Introduction to Abbot
- Java Kicks Ruby on Rails in the Butt
- Interviewing Java Developers With Tears in My Eyes
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- How to Diagnose Java Resource Starvation
- REA Is Where RIA Becomes the Norm
- Kindle 2 vs Nook
- Anatomy of a Java Finalizer
- Why IBM’s Server Chief Got Busted
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?




































