JavaFX Script for Java developers, Part 1


First touch

The main method in JavaFX is called run().

function run(args: String[]) {
    // implementation
}

Let’s print something to the output. System.out.print() is simply print() in JavaFX.

function run(args: String[]) {
    print("Hello from JavaFX!!");
}

Moreover run() is not mandatory for the developer :

print("Hello from JavaFX!!");

As simple as it gets; all scattered statements in a script file are properly compiled and executed.

Variables

Variable declaration is done in a concise way. This is how to declare an Integer,

var age = 7;

a Double,

var price = 7.0;

and a Boolean.

var isCool = true;

The following program that prints true three times,

var age = 7;
var price = 7.0;
var isCool = true;

println(age instanceof java.lang.Integer);
println(price instanceof java.lang.Double);
println(isCool instanceof java.lang.Boolean);

may also be restated as :

println(7 instanceof java.lang.Integer);
println(7.0 instanceof java.lang.Double);
println(true instanceof java.lang.Boolean);

Speaking of new features, JavaFX has an extra arrow in its quiver :

var duration = 7ms;
println(duration instanceof javafx.lang.Duration);

Finally, to declare a final variable you may change var with def.

def steady = 100;

Methods

A void method is declared as follows.

function say() {
    print("hello!");
}

This is how to specify arguments.

function say(something: String) {
    print(something);
}

Of course overloading does exist, so the following is a valid program.

function say() {
    print("hello!");
}
function say(something: String) {
    print(something);
}
say();
say("Hey Niko!");

Finally, a return type is added at the end of the method signature.

function square(data: Number): Number {
    return data * data;
}

Class

Let’s design persons. For every person we’re interested about his name and age.

public class Person {
    public var name: String;
    public var age: Integer;
}

Of course you may encapsulate the state of a JavaFX class according to the JavaBean specification (private fields, getters, setters), but the above syntax allows handy initialization of objects.

Here’s the definion of a car.

public class Car {
    var brand: String;
}

Maybe a Person should HAS-A Car.

public class Person {
    public var name: String;
    public var age: Integer;
    public var car: Car;
}

Objects

Let’s create a person :

Person {
    name: 'Michael Jordan'
    age: 46
}

Using Java, this is equivalent to an anonymous inner class.

// Java
new Person {
    setName("Michael Jordan");
    setAge(46);
}

To give the instance a name:

var person = Person {
    name: 'Michael Jordan'
    age: 46
}

In Java this may also be expressed as:

// Java
Person person = new Person();
person.setName("Michael Jordan");
person.setAge(46);

At this point, Michael Jordan needs a good car.

var person = Person {
    name: 'Michael Jordan'
    age: 46
    car: Car {
        brand: 'BMW Z4 Roaster'
    }
}

Just to be clear, in Java this would be:

// Java
Car car = new Car();
car.setBrand("BMW Z4 Roaster");

Person person = new Person();
person.setName("Michael Jordan");
person.setAge(46);
person.setCar(car);

Wrap up

JavaFX Script is a visual-oriented language for creating rich applications. As an advantage, it’s built on the powerful basis of Java. Moreover, as we’ll experience in the next part, its syntax is simpler and more expressive.

Thank you.

6 Responses to JavaFX Script for Java developers, Part 1

  1. Erundassaups says:

    Очень интересно!!! Только не очень могу понять как часто обновляется ваш блог?

  2. UNUBREHEERIE says:

    Интересно и позновательно, а будет еще что-то по этой теме?

  3. LIBEGIKE says:

    Спасибо за статью.. Актуально мне сейчас.. Взяла себе еще перечитать.

  4. […] to the JavaFX language: part1 and […]

  5. Очень полезная вещь, спасибо!!

  6. Спасибо за познавательную статью!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: