Posts Tagged ‘JavaFX’

JavaFX Script for Java developers, Part 2

6 February 2009

Comfortable arrays

Arrays in JavaFX are easy-to-use and act more like sequences. Here’s one:

var letters = ['A', 'B', 'D'];

Let’s print it,

println(letters);

Displaying an array in JavaFX

get its size,

println(sizeof letters);

Getting the text of an array in JavaFX

reverse it,

println(reverse letters);

insert an element at the end,

insert 'E' into letters;
println(letters);

insert more elements at the end,

insert ['F', 'G'] into letters;
println(letters);

and insert something at the middle.

insert 'C' after letters[1];
println(letters);

Click here to read more…

JavaFX Script for Java developers, Part 1

25 January 2009

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.