From internal collaboration to supplier and customer interactions, enterprises are discovering new ways of increasing productivity, process accountability, and connecting those challenging "white spaces" that exist betwe...| By James L. Weaver | Article Rating: |
|
| November 23, 2007 08:00 AM EST | Reads: |
14,093 |
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 14,093
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? |
||||
- Whatever the Apple iPad Is, It Apparently Leaks Like a Sieve
- Reflections on Java Command Line Options
- Six Enterprise Megatrends to Watch in 2010
- Microsoft WebsiteSpark: Get New Business Leads to Grow Your Business
- Jobs Has a Few Words for Google & Adobe & They Ain’t Pretty: Reports
- Stealth Cloud Computing Startup To Launch at Cloud Expo
- Apple iPad Reminds Us How Brands Succeed by Transforming Experiences
- Apple iPad in 3rd Place Behind iPhone and Android For Application Developers
- PivotLink Named Cool Cloud Computing Vendor
- iPad on Ulitzer - I’ll Buy iPad. But What For?
- Adobe Flash on the Road to Nowhere
- SAP Wants CA To Give It More Toys
- Whatever the Apple iPad Is, It Apparently Leaks Like a Sieve
- Reflections on Java Command Line Options
- Six Enterprise Megatrends to Watch in 2010
- Microsoft’s First Step Toward Cloud Computing
- Microsoft WebsiteSpark: Get New Business Leads to Grow Your Business
- Jobs Has a Few Words for Google & Adobe & They Ain’t Pretty: Reports
- Stealth Cloud Computing Startup To Launch at Cloud Expo
- Apple iPad Reminds Us How Brands Succeed by Transforming Experiences
- Apple iPad in 3rd Place Behind iPhone and Android For Application Developers
- PivotLink Named Cool Cloud Computing Vendor
- How to Secure REST and JSON
- iPad on Ulitzer - I’ll Buy iPad. But What For?
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX
From internal collaboration to supplier and customer interactions, enterprises are discovering new ways of increasing productivity, process accountability, and connecting those challenging "white spaces" that exist betwe...Mar. 21, 2010 08:23 AM EDT Reads: 108 |
By Roger Strukhoff 3rd Generation outsourcing is here! 1st Generation was “your mess for less”; 2nd Generation is strategic or selective sourcing, including hosting. 3rd Generation Outsourcing, as a result of the emergence of Cloud Computi...Mar. 21, 2010 05:53 AM EDT |
By Pat Romanski NaviCloud is a next-generation platform that combines the economic efficiencies of cloud computing with true enterprise-class reliability and security. With built-in high-availability, a state of the art operations cente...Mar. 20, 2010 08:45 AM EDT Reads: 540 |
By Pat Romanski SYS-CON Events announced today that VirtuDataCenter, a cloud computing network infrastructure company, will offer a complete turnkey alternative to today’s cloud computing solutions. They will exhibit at SYS-CON's 5th In...Mar. 20, 2010 07:00 AM EDT Reads: 337 |
By Liz McMillan You are interested in cloud computing, but where do you start? How are vendors defining Cloud Computing? What do you need to know to figure out which applications make sense in the cloud? And is any of this real today?
...Mar. 20, 2010 05:45 AM EDT Reads: 389 |










3rd Generation outsourcing is here! 1st Generation was “your mess for less”; 2nd Generation is strategic or selective sourcing, including hosting. 3rd Generation Outsourcing, as a result of the emergence of Cloud Computi...
NaviCloud is a next-generation platform that combines the economic efficiencies of cloud computing with true enterprise-class reliability and security. With built-in high-availability, a state of the art operations cente...
SYS-CON Events announced today that VirtuDataCenter, a cloud computing network infrastructure company, will offer a complete turnkey alternative to today’s cloud computing solutions. They will exhibit at SYS-CON's 5th In...
You are interested in cloud computing, but where do you start? How are vendors defining Cloud Computing? What do you need to know to figure out which applications make sense in the cloud? And is any of this real today?
...
Cloud Computing Journal caught up with the CEO of a major new player in the fast-emerging Cloud ecosystem - a CEO who has taken an interesting and unusual decision. While signing up as the Platinum Plus Sponsor of the 5th International Cloud Expo, he and his company have decided to remain completely...
Enterprises continue to expand the use of cloud computing, and particularly software-as-a-service applications (SaaS), to achieve operational performance enhancements and efficiencies. Implementation of these technologies introduces several challenges related to identity management, such as administ...
But, as much as I like developing nations and the potential of cloud for them, the Big Kahuna is still found in the Big Apple, with Cloud Expo, opening April 19 at the Javits Center in New York. This is not an event with a single track, or a few tracks. There are, in fact, eight of them, as follows:...
Is your website available to end users 99.8% or more of the time? If not, then count yourself in the “laggard” category, according to standards set by The Aberdeen Group, in its 2008 report “The Performance of Web Applications: Customers are Won or Lost in One Second.” In that study, laggards had we...























