Using JavaFX Mobile to Combat Device UI Fragmentation

JavaFX 1.1, a powerful tool for building rich UIs across a multitude of clients, provides mobile emulation for developing JavaFX Mobile applications. Get a beginner's guide to JavaFX Mobile development.

by Jim White

Apr 3, 2009

Page 1 of 5

WEBINAR:

On-Demand

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

ith the recent release of the JavaFX Software Developer Kit 1.1, Sun also released the long awaited JavaFX Mobile platform. Through a Java-like scripting language (JavaFX Script) and a single API, JavaFX provides a development tool that supports a variety of platforms, from desktops to mobile devices.

This article provides a beginner's guide to JavaFX Mobile development, but because all JavaFX development relies on the same SDK and tooling, it could just as easily be about JavaFX in general. Rather than offering a separate JavaFX Mobile SDK, the JavaFX SDK 1.1 and its development tools provide mobile emulation for developing JavaFX Mobile applications. Joshua Marinacci from Sun's Java Swing team put it best in a recent blog posting: "There is no JavaFX Mobile. There is only JavaFX."

Java's ubiquity on mobile devices has made the "write once, run anywhere" (WORA) premise difficult to abide by given the thousands of mobile device screens. With JavaFX Mobile, the Java community has a tool to possibly combat this device UI fragmentation.

JavaFX's Syntax and Synergy with Java

JavaFX is a product framework comprised of JavaFX Script and JavaFX Mobile. JavaFX Script (JavaFX for short) is a scripting language meant to provide rich user interfaces and as its name implies, JavaFX Mobile is JavaFX for mobile platforms.

JavaFX's declarative style is more apparent when contrasted with the imperative programming style of Java Swing. The following Swing code creates the same "hello world" window as the previous JavaFX example (again minus imports):

Notice the imperative nature (i.e., statement by statement, control flow) of Swing, which has been used for traditional Java UI development. Figure 4 shows the result of the Java Swing code.

Figure 4. Swing Versus JavaFX: This is the same "Hello World" window created from Swing code versus JavaFX code.

As previously stated, JavaFX is neither Java nor JavaScript, but JavaFX scripts can call Java classes or be called on by Java classes. For example, in the following JavaFX script for determining the size of a window, the Stage object's size has been rewritten to use the standard Java Math class:

Sun has touted JavaFX's ability to leverage existing Java code as one of its advantages over other RIA technologies such as Adobe Flex. Also, while JavaFX Script provides a platform for building any application, its purpose as defined by Sun is for use in UI development and not for business or backend components.