Share

From this page you can share JavaFX: The Second Coming of Swing to a social bookmarking site or email a link to the page.
Social WebE-mail
Enter multiple addresses on separate lines or separate them with commas.
JavaFX: The Second Coming of Swing
(Your Name) has forwarded a page to you from Ajaxonomy
(Your Name) thought you would like to see this page from the Ajaxonomy web site.

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.