Last Updated:

JavaFX in action

While Flash and Silverlight are heavily covered by the computer press, the competing JavaFX technology for some reason is not so popular with journalists. "Computer News" corrects this state of affairs and offers you a story about the practical use of this promising technology.


Introduction

Since in the last issue of "Computer News" I have already talked about what exactly is hidden behind the name JavaFX, I will not repeat myself. Let me just remind you that this is a technology for building RIA applications proposed by Sun Microsystems.

The most important question to answer is how is JavaFX different from Java? If you answer it briefly, the answer will be simple: everyone. Starting at least with the fact that JavaFX uses not Java as a programming language, but a special JavaFX Script. Of course, this doesn't mean that you can't use Java code in JavaFX applications.

The second important issue that concerns JavaFX is the differences between this platform and Flex and Silverlight. The difference is mainly in what platforms and tools different developers are used to. If Flash is closer to you, then it will be more convenient with Flex, if .NET - with Silverlight, but for those who are accustomed to dealing with Java, JavaFX will be more convenient. Of course, when it comes to trifles, more differences are revealed, but if you look at these platforms, as they say, from a bird's eye view, then in terms of their capabilities they are approximately equal.


Tooling

Well, let's get to know JavaFX firsthand. In order for it to take place, you and I will need to download and install tools, without which programming in JavaFX is simply impossible.

The first is, of course, the JDK (Java Development Kit). Although JavaFX and Java, as I just said, are quite significantly different from each other, the former is still based on the latter, and therefore it will not be possible to do without the JDK, moreover, one JDK, due to the mentioned differences. Therefore, you and I will also need the JavaFX SDK. In theory, a separate JavaFX Script compiler should be enough, but, frankly, I do not think that this will be much more convenient than using the SDK. Both the JDK and the JavaFX SDK can be found on the Sun Microsystems website, so I won't provide full links so as not to clutter up the article.

It's also probably a good idea to have some kind of development environment that would allow you to create applications using JavaFX with the greatest possible comfort. You can currently use JavaFX in both NetBeans and Eclipse. Both the NetBeans IDE itself and the Eclipse plug-in can be found on the www.javafx.com website. As for such a popular environment as IDEA (many people like it more than Eclipse, and I can guess why), there is no plugin for it that would allow you to add JavaFX support to this environment.


JavaFX Scripting Language: Introduction, or "Hello World"

 

Well, we believe that everything that was needed, you installed, and now you can get acquainted with the scripting language itself, which is used when creating JavaFX applications. It is generally quite similar to Java in terms of coding syntax, however, of course, there are also a lot of differences.

The first difference is the declarative nature of the language. JavaFX Script is a language "sharpened" for the creation of interfaces, and not for solving common problems of various orders, like Java. Accordingly, the descriptions of the interface in JavaFX look quite simple. You can see the traditional "Hello World" app in the listing.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
Stage {
 title: "Hello World"
 width: 250
 height: 80
 scene: Scene {
  content: Text {
   font : Font {
    size : 24
   }
   x: 10, y: 30
   content: "Hello World"
  }
 }
}

Note that due to the declarative nature of the language, there is not a single line of code in this listing that would execute any instruction, however, despite this, the program does the same thing that any program from the category of "Hello World" should do - that is, displays this phrase to the user as a greeting.


Variables and functions

However, all this does not mean that in JavaFX you can not write ordinary, normal program code that does something useful. In JavaFX, like any other programming language, there are things like variables and functions.

Variables in JavaFX are declared using the var and def keywords. For example: "def const1 = 100; var variable1; var variable2" (without quotation marks, of course). How are these ads different? In principle, I think the difference is quite obvious. Although in JavaFX terms these are all variables, in practice it turns out that what is declared with def is a variable that cannot change its meaning, sitting as a constant. What is declared by var, by contrast, both smells like a variable and behaves accordingly.

By the way, I want to note that both variable names and named functions in JavaFX use the same conventions as in Java - that is, when the name of a variable or function contains several words, the first is written with a lowercase letter, and the rest - with a capital letter. For example: thisIsVariable, thisIsFunction.

As for functions, they in JavaFX look and work the same way as everywhere else: you have some input parameters, you do something with them and return some value. For example, as it is done in the listing.

function add(argOne: Integer, argTwo: Integer) : Integer {
 result = argOne + argTwo;
 println("{argOne} + {argTwo} = {result}");
 return result;
}

In it you can see a function that deals with the summation of two numbers and the display of the result on the screen.


Objects

When we say Java, we mean object-oriented programming. Say JavaFX... Hmm, something OOP still we haven't seen here. Did Sun Microsystems decide to make a purely procedural language after Java? That is, excuse me, procedurally declarative. Turns out, not really. Without facilities in today's IT world, neither here nor there. There is no getting away from them and those who decided to use JavaFX in their work.

I must say that objects in JavaFX and objects in Java are superficially more than similar. Here and there they are instances of special data types - classes. The difference in the declaration of classes is, perhaps, only in the different declaration of their members - instead of C-like "int i" in Java, we see Pascal's "var i: integer". An example of a class written in JavaFX Script can be seen in the listing.

class Customer {
 var firstName: String;
 var lastName: String;
 var phoneNum: String;
 var address: Address;
 function printName() {
  println("Name: {firstName} {lastName}");
 }
 function printPhoneNum(){
  println("Phone: {phoneNum}");
 }
 function printAddress(){
  println("Street: {address.street}");
  println("City: {address.city}");
  println("State: {address.state}");
  println("Zip: {address.zip}");
 }
}

Classes can be inherited from each other (fortunately, as in Java, inheritance is single). You can override methods and do other things that those who are used to the object-oriented programming style are used to. There are also the usual Java access modifiers protected and public. There are other, no less useful - public-read and public-init. As written in the official documentation, "the public-read access modifier defines a variable that is readable from anywhere, but is writable (by default) only available from the current script. The public-init access modifier defines a variable that can be publicly initialized by an object constant contained in any package."

Most of what is related to classes, I liked the declarations of instances of classes in JavaFX. In fact, you and I have already encountered it - in the very "Hello World" application that you can see in the first listing. In the listing, you can see a similar declaration for an instance of the Customer class just discussed from the previous example.

def customer = Customer {
 firstName: "John";
 lastName: "Doe";
 phoneNum: "(408) 555-1212"
 address: Address {
  street: "1 Main Street";
  city: "Santa Clara";
  state: "CA";
  zip: "95050";
 }
}


Subtotals

 

Well, let's sum up - so far intermediate - what was said above about JavaFX. Why intermediate? Because we will continue a fascinating conversation about JavaFX - after all, in fact, we have just begun to get acquainted with this interesting and promising technology and did not even have time to fully consider the scripting language underlying it.

This language, as you can see, is different from Java, however, fortunately, these differences do not make it difficult to understand and use. Like Java, JavaFX Script offers convenient tools for working with objects, but at the same time it has very interesting declarative features that will certainly not remain unclaimed when actually creating RIA applications. In general, we can say that JavaFX Script is as simple and as effective as Java itself. And, probably, it would even be strange if a company that managed to create such a great programming language as Java, a scripting language for RIA applications, turned out to be frankly weak.