JavaFX: The Second Coming of Swing

Tagged:  

At JavaOne 2007, Sun announced with much fanfare its JavaFX platform including two major components: Mobile and Scripting. This article will focus on the most interesting aspect of this new platform (for Swing old-timers, at least), the Java FX Script component. For interested parties, all of the relevant files can be found in the OpenJFX project hosted on java.net. The distribution is currently just a dump of the Subversion tree, but contains plenty of goodies to get started with the language. For those who are merely interested in sampling what Java FX can do, there are plenty of JNLP-based demos which can be launched from the main page (a 1.5+ JRE is required to be installed on the client).

The First Coming of Swing

Some of us may remember the first time Sun introduced AWT (later to evolve into Swing) to an excited world in 1995. It wasn't long before many developers were eagerly creating Applets in their web browsers, marvelling at the prospect of code being streamed over the network and executed in their browser. Applets were cool. Applets were hot. Applets were...well, ugly. Back then, though, Windows 95 defined computer aesthetics, and Applets didn't suffer too much in comparison.

Fast forward several years, and the picture has changed rapidly. Swing is no longer the disruptive technology that it once seemed to be. After Microsoft began neglecting its Java implementation for Windows/IE (coincidentally after a long battle with Sun over its embrace-and-extend tactics) and Netscape lost the browser wars, Swing had become less important as a technology. Most web developers moved on to using Flash, Java moved into its server-side stronghold (having failed to really take off on the desktop), and Swing was left to stagnate. Swing continued to improve over the years, but Sun's--and most of the world's--attention had moved elsewhere.

Enter JavaFX

In 2005 Sun acquired SeeBeyond Technology Corporation, an employee by the name of Chris Oliver, and a little creation of his called F3 (Form Follows Function), later dubbed JavaFX Script. Sun saw its opportunity to position itself in the Rich Internet Application (RIA) market, and set its sights on competitors, Microsoft's Silverlight and Adobe's AIR (Flex). It's too early to tell what the success of JavaFX Script will be, but Swing has come a long way since the heady early days of the Internet and JavaFX may be just the trick in smoothing some of the pain points in developing with Swing.

Introduction to JavaFX Script

To veteran Java developers, JavaFX Script will seem both familiar and strange at the same time. Depending on which style of the language you develop in, it can look either like JavaScript or like "CSS with scripting". What follows is an example, written in both styles, with the obligatory "Hello World":

Frame {
title: "Hello World Frame"
width: 250
content: Label {
text: "Hello World"
}
visible: true
}

Figure 1a: Hello World in "CSS with Scripting" style

Or

var f = new Frame();
f.title = "Hello World Frame";
f.width = 250;
var label = new Label();
label.text = "Hello World";
f.content = label;
f.visible = true;

Figure 1b: Hello World in JavaScript style

Run either of these examples in the JavaFXPad application (provided in the distribution or via JNLP on the web site), and you will see the familiar Swing frame with its content as a single label containing the text "Hello World". It isn't hard to see that JavaFX is much more compact than the equivalent Java code. That in itself addresses one of the major difficulties in developing in Swing, but JavaFX Script is more than that. The language contains "shortcuts" that make more sophisticated development in Java 2D and 3D as easy as doing it in a declarative language such as SVG or Flex. The default code displayed when JavaFXPad launches demonstrates this convincingly, showing how easy it is to do scaling, transforms, or create a shadow filter.

Core Language

JavaFX Script contains a lot of syntactic similarities to both Java and JavaScript, so many constructs will already be familiar to an experienced developer. This section will highlight some of the important differences and unique features of JFX Script.

Primitives Values

JFX Script has four primitives: String, Boolean, Number, and Integer (all whole number values, i.e., byte, int, long, etc.).

Operators

JFX Script replaces the more C++-like !, &&, and || with not, and, and or.

For Loops

JFX Script has a version of the "foreach" constructs that allows iteration over an interval, like so

for(i in [0..5]) {
//do something
}

Operations and Functions

JFX Script has two different keywords to declare methods: operation and function. Funcions in JFX Script are forbidden to have loops or conditional operands in order to minimize potential side-effects. Why is this needed? Because a function re-evaluates its return value every time one of its input parameters or referenced variables is changed. This turns out to be very handy when you want to bind some output to values that frequently change. Operations are more like normal Java methods.

IDE Support

Obviously, the JavaFXPad application is insufficient for real-world development, so early versions of plugins for using JFX Script with Netbeans and Eclipse are available. Of the two, the Netbeans plugin seems further along in its development (it is a Sun technology, of course ;) ), but expect this to change as the project nears release. See the OpenJFX site for downloads and instructions.

Conclusion

JavaFX Script is still very much in pre-release stage, but is already looking like an extremely promising technology. Currently, it runs in interpreted mode but a compiler is being developed for the language that will compile scripts straight into Java bytecode, giving the same performance as the equivalent Swing code with a fraction of the effort. It definitely has an uphill battle in the race for the RIA market, but for existing developers that use Swing it is already a winner.

Brennan,

It's great to see posts like this. I agree that JavaFX holds great promise for many reasons, including the one that you pointed out here -- that simple, but powerful, declarative scripting can define Java Swing UIs. There are two resources in addition to what you mentioned that I'd like to point out:

1) The PlanetJFX site at http://jfx.wikia.com, and if you'll permit me:

2) My JavaFX blog http://learnjavafx.typepad.com, whose stated purpose is to help people "Learn JavaFX Script and Other JavaFX Technologies"

Thanks,
Jim Weaver
Author of "JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications" (Apress)

@Jim,

Thanks for the post. A really great resource that I forgot to include is the Learning Curve Journals being posted by Sun employee John O'Connor. The link to the first in the series is here:
Part I

Links to subsequent parts are there on the right side of the page.

/Brennan

The Netbeans plugin is opensourced and in active development. Eclipse one is just going to be opensourced... See javafx.netbeans.org

Hi,

I think we need larger community of JavaFX.
Here it is,
www.javafxworld.net

Share,Discuss and vote for Blogs, Articles, Videos, Books .

Subscribe to get updates on latest posts. It will keep you updates about the happenings in JavaFX World.

Regards,
Raj

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <div> <blockquote> <object> <embed> <img> <param>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.