Rather than use this article to detail the syntax and API of this new script language, I have given you a couple of example applications and point you to two OpenJFX pages that outline and document the syntax of JavaFX. Probably the best "informal documentation" on the basic syntax and structure of a JavaFX application is found here
. After you have mastered the basic syntax, check out the online tutorial here
. That page demonstrates the graphical user interface components in detail. Finally, if you are looking for the basic API documentation, check out the Javadoc-like set here
. JavaFX is in its early stages. So documentation is still shy of what you would expect for other Java APIs or technologies, but these sources will let you start to discover JavaFX and evaluate its future.
As mentioned, attached to this article are a few JavaFX files/applications that are discussed and demonstrated below. These should also help you explore JavaFX.
The JavaFX Application
Where will JavaFX applications run? Sun's hope is that JavaFX will run everywhere. Anywhere there is a Java runtime, JavaFX applications should be able to be run. In the smaller platform arena, JavaFX Mobile is a Java/Linux mobile and embedded operating system that Sun plans to distribute as a binary operating system to device manufacturers. Obviously, this platform is devised to host JavaFX apps. JavaFX Mobile is based on technology developed by SavaJe Technologies, which Sun purchased in April 2007.
Exploring the JavaFX LanguageDeclarative Syntax
Sun intends JavaFX to offer more rapid user interface development than pure Java. While this scripting language can be used for a whole host of application needs, it seems fitting to start to explore the JavaFX scripting language and its features from its point of strength. Namely, let's look at using JavaFX to develop a user interface; in particular a login window. Again, JavaFX code is written in a file with a .fx extension. Compare the two files in (Listing 1 and Listing 2). Both create a login window (see Figure 5). The first uses JavaFX. The other uses traditional Java Swing. You might note some similarities in the class names used, but obviously the structure and makeup of JavaFX is quite different.
|Figure 5. The Login Windows: On the left is the result of executing the Login.fx application. On the right is the result of running the more traditional Java Swing application Login.java.|
This small example highlights the first major difference between JavaFX and plain Java. JavaFX uses declarative syntax like HTML to create the user interface. In a declarative syntax, you the developer specify what something is to look like rather than to write an algorithm on how to create it. The style of development that you have become familiar with in programming languages like Java is called imperative (also often called procedural) programming. Typically, declarative programming is easier to code and maintain, especially when building user interfaces that focus on the look.
It should be noted that you don't have to use the declarative structure. You can create the exact same login window with JavaFX code using a procedural syntax (see Listing 3). This code uses procedural syntax, but results in an identical window display.
So, what is going on in the JavaFX code? The procedural form of the Login.fx file may help you to see, initially,what the declarative syntax defines. Just like in standard Java applications, the first line imports the JavaFX user interface components (more later). Then, an instance of a Frame is created, its properties are set, and it's displayed or made visible to the user. In particular, the frame's content is set with child elements of a GridBagPanel, composed of a set of cells each of which has a set of cells which each contain a label, entry field, or button widget. If the names or types of the "widget" elements sound familiar, that is because the JavaFX is closely aligned with Swing. The JavaFX Frame object wraps javax.swing.JFrame. The labels (SimpleLabel) are wrappers for javax.swing.JLabel. In fact, it is precisely because JavaFX is based on Swing that it offers a rich user interface capability (with a relatively low learning curve), but the declarative syntax helps remove some of the complexity.