Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage Articles Java FX Technology Preview

Java FX Technology Preview

This item in japanese

The success of Java revolves around the fact that it is a good, general purpose language within its problem domain. For writing long running server-side code typical in modern Enterprise Computing, and for other situations such as mobile development where cross-platform support, stability and security are key factors, Java represents an excellent choice. There are however some programming tasks that benefit from a degree of specialisation and for which Java, in common with other general purpose languages, is less well suited. An example is modern GUI design which raises both technical and managerial challenges.

On the managerial side a slick, complex User Interface typically requires a number of different roles – graphics artists, information architects, animators and programmers. Whilst some people may be able to fill all of these, such people are rare; more typically a number of different people will be involved in the process and the flow of resources back and forth between designers and developers can become a significant problem. On the technical side modern UI design often requires a high level of technical knowledge; a sophisticated toolkit like Swing confronts a developer with a complex set of controls they need to learn how to use when creating the UI. Furthermore the responsiveness of the interface depends heavily on concurrency. This isn't in itself a problem - multithreaded programming in Java is straightforward - but does result in a large amount of boilerplate code to manage transitions and animations.

Pitched into the Rich Internet Application space as a competitor to Adobe’s Flex and Microsoft’s Silverlight, Sun’s JavaFX is one part of Sun’s strategy for addressing these issues for Java developers. JavaFX aims provide a new foundation platform for building Rich Internet Applications across desktop, internet and mobile devices. It also represents a significant shift in the way Sun engages with the Java product market, seeing them building up a complete solution rather than focusing solely on the underlying technology. The current technology preview comprises two major components: Project Nile, which focuses on designer/developer workflow, and JavaFX Script, a new declarative language for writing Java GUI applications.

Whilst both Flex and Silverlight use XML as their declarative language (MXML and XAML respectively) Sun have chosen to develop a new scripting language, JavaFX Script, for the task. Sun Staff Engineer Joshua Marinacci told us that Sun have no plans to add an XML dialect at this point:

"We have found a lot of people really hate using XML and wanted a more compact declarative syntax optimized for graphical interfaces. We think developers familiar with JavaScript will find JavaFX Script pretty easy to pick up."

JavaFX Script is a declarative, statically typed, compiled, Domain Specific Language (DSL) for creating user interfaces on top of the Java Standard and Micro Editions, with current Java packages accessible from the JavaFX environment. It targets two distinct user groups:

  1. Java developers who are already familiar with Swing and are looking for a way of building feature rich interfaces in a faster, more productive way.
  2. Web developers who are more familiar with other scripting languages such as JavaScript or ActionScript.

JavaFX Script has strong productivity features including some convenient predicate features for insert and delete operations (insert 10 before x[1]; for example). Other common problems are elegantly solved: binding UI components to a backend database is straightforward, for instance, and the language supports firing an event when the value of a variable changes, through a simplified listener-like mechanism referred to as an event trigger.

Sun has put a great deal of emphasis on the readability of the language making it easy to pick up and follow other developers' code. One consequence of this however is that they have used a BASIC-like set of boolean operators in place of the C-style operators used in both Java and JavaScript. So for example JavaFX Script uses 'and' for &&, and 'or' for ||. It also supports 'not' although support for '!' has been added back in as well. It really doesn't take long to get used to this but given the target audience I did find it surprising.

There are a couple of other surprises in the language too. One is that whilst it supports the creation of a variable or function using a currently reserved word, it requires you to escape them using French quotes - for example var <> = 100 - rather than working this out from context parsing. Another is that that JavaFX Script chooses to support multiple inheritance in place of Java's interface approach. According to Marinacci:

"We found in the context of GUIs it makes certain tasks a lot easier. In most cases, however, developers won't have to do any subclassing, so it won't be an issue. Subclassing is mainly used for component developers."

JavaFX overhauls a number of ideas familiar from Java itself. One worthy of attention is the Javadoc equivalent JavaFXDoc which makes some much needed updates to the documentation tool. In place of HTML, JavaFXDoc produces an XML format document which can then be transformed into a secondary output format. The current format is XHTML 1.0 but this two step process offers a degree of future proofing opening up the possibility of support for other formats such as PDF, or a database to build up a knowledge base. The new output format doesn't use frames, and can easily be skinned using CSS. It also allows for some basic filtering for individual profiles – so for example the JavaFX APIs are split into common, desktop and mobile profiles and the documentation can be filtered as appropriate. There is an example of the output here. The focus on productivity is evident here too with JavaFXDoc supporting inline examples via an @examples tag. Example code is output with syntax highlighting automatically generated, and the doc tool then compiles and executes the example putting a screen shot into the generated document thus providing an automated way of keeping documentation screen shots up-to-date.

We talked to Marinacci about future plans for the tool and he confirmed that Sun were working on a search solution, although this isn't expected for the version 1 release. An update to the core Javadoc tool is on the provisional list for Java 7 and he confirmed that a number of the lessons learnt from the work on JavaFXDoc will be applied to future versions of Javadoc.

JavaFX Script has access to all the existing Java packages but also includes new APIs. The two key ones are the media APIs (currently provided as native libraries that will be supplemented with the cross-platform On2 TrueMotion video codecs in the version 1 release) and the scene graph API. The scene graph API represents something of a shift in how developers from a Swing background typically think about GUI. Here the GUI is represented as a hierarchy of visual nodes that make up a scene and respond to user interaction. The scene graph model supports effects, arbitrary transformations (scaling and rotations), and animation. The scene graph retains the graphics objects and draws them on the scene whenever a refresh happens thereby abstracting away the details of handling repaints, caching, and many other details familiar from working with immediate mode APIs like Java2D. This level of abstraction also allows the API to do all sorts of automatic optimisation as well such as preloading textures and primitives to the graphics card. The scene graph API is closely associated with JavaFX but you can also use it in ordinary Java applications.

Sun has split the APIs into three profiles two of which are available now: The common profile is for all the APIs that are available on every device and includes the scene graph API and media components, whilst the desktop profile represents the more familiar Swing components. The third is the mobile profile which is expected to be released during 2009 and is expected to be a complete application stack for mobile handset developers but the publically available details remain sketchy.

There are one or two significant omissions from the common API at the time of writing – for example there are no equivalents to the Table, TabbedPane, TextArea, and BookPanel components in the JavaFX preview version. This release version should feel a little more complete and consistent. Marinacci told us:

"We are planning to move Swing components to subclass Node, so you can freely mix Swing components with graphics nodes without using the adapter classes. For the time being advanced classes (like tree and table) will on Desktop only."

The decision to develop a new language rather than following Adobe's lead here is a bold one and I think a good one. JavaFX Script is generally very pleasant to work with and has a number of possible uses beyond its current application - for example the JavaFX data format (FXD), a key part of project Nile, uses a subset of the JavaFX Script declarative format to represent graphic resources.

With Silverlight, Microsoft delivered its own toolset (the Expression suite) to directly take on Adobe's Creative Suite offerings. Sun is also working on its own tool (expected next year) but has chosen via Project Nile to first focus on supporting the popular existing tools starting with Adobe's Illustrator and Photoshop.

Supported on both OS X and Windows, Project Nile includes plugins for Adobe Illustrator and Photoshop CS3, an SVG converter, and a viewer for the JavaFX data format (FXD). A designer working with one of the Adobe tools creates their graphic resource in the usual way and can then export the file to FXD. The viewer utility allows users to view the graphics file to confirm that the exported file looks as expected. From within the JavaFX scripting language developers are able to access and manipulate the individual layers that make up a single graphic resource file. This works well for graphic resources but still leaves the problem of applying custom styling to the basic components. Here Sun is looking to CSS and the ability to skin Swing components via CSS is targeted for the version 1 release of JavaFX.

Underpinning all of this work is Java 6 Update 10 (formally the consumer JRE project) – improvements to deployment, performance, memory usage and the built in look and feel (Nimbus) all help the effectiveness of Swing as a desktop development platform. Unfortunately this is quite a limiting factor for developers at the moment.   Whilst Sun has shipped versions of update 10 for Linux and Solaris as well as Windows there are crucial implementation differences between the versions with the applet quickstarter, kernel installer, and direct3D pipleine all restricted to the Windows platform only. Moreover Apple have chosen to restrict Java 6 in any form to OSX 10.5 on 64-bit Intel Macs. Marinacci did tell us that Sun is working with Apple to improve Java and JavaFX support on OSX although didn't provide any details. He also gave us up-to-date download figures:

"We average 40-50m downloads of the JRE each month, with some months peaking at 100m. > 95% of these downloads are JavaSE 6. A couple of months after JavaSE 6 update 10 goes final it will be turned on in the auto-update center and we expect to see similar numbers for that as well. We also know that 91% of internet connected computers have Java installed according to Omniture."

That both Flash and Silverlight are entirely closed source proprietary tools remains a significant barrier to adoption in some developer shops. Moreover given that so much of the key code in Flash, such as its audio and video codecs, is licensed from third party sources it would be very difficult for Adobe to open up the platform in the way Sun did with Java. Although the licenses are not yet finalised Sun has committed to making JavaFX available as open source software, and has so far released the compiler and scene graph APIs under the GPL v.2 license. This may well boost its adoption when the platform is released but it still has significant work to do to become a strong contender.

Rate this Article