ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Introduction to JavaFX Script
Pages: 1, 2, 3, 4, 5, 6

JavaFX Operators

The well-known Java operators &&, ||, and ! are displayedlike this in JavaFX:

  • Java: &&
    JavaFX: and
  • Java: ||
    JavaFX: or
  • Java: !
    JavaFX: not

JavaFX Functions

JavaFX supports functions. Here are example simple functions with arguments, variable declarations, and a return statement.

function taxes(x) {
    var t:Number = (19.0/100.0)*x;
    return t;
    }

JavaFX if Statement

In JavaFX you can use conditional blocks by using the if statement. Curly braces are required with this statement . If the else clause is another if statement, then you can skip curly braces:

if (place_your_condition_here) {
        //do something
    } else if (place_your_condition_here) {
        //do something
    } else {
        //do something
    }

JavaFX while Statement

The while statement is similar to while in Java. Curly braces are always required with this statement.

while (place_your_condition_here)
    {
    //do something
    }

JavaFX for Statement

The for statement can be used to loop over an interval (intervals are represented using brackets [] and the .. symbol).

//i will take the values: 0, 1, 2, 3, 4, 5
for (i in [0..5])
    {
    //do something with i
    }

JavaFX Procedures

JavaFX procedures are marked by the operation keyword. Here is a simple example:

operation startClock() {
do{
    while(true)
        {
        if(seconds>=360)
    {seconds = 0; seconds = [0,6..360] dur 60000 linear;}
        if(minutes>=360)
    {minutes = 0; minutes = [0,6..360] dur 3600000 linear;}
        if(hours>=360)
    {hours = 0; hours = [0,6..360] dur 43200000 linear;}
        }
    }
}

JavaFX Classes

JavaFX classes are marked by the class keyword. A JavaFX class may extend more classes by specifying the extends keyword and a comma separated list with the names of those classes. Between curly braces you can place attributes, functions, and operations, like in the next example:

class Order {
    attribute order_nr: String;
    attribute ordertype: Order inverse Order.products;
    attribute products: Order* inverse Order.ordertype;
    function getOrderNr(): String;
    operation addOrder(order: Order);
    }

Notice that attributes are declared using the attribute keyword and that the body of the function and procedures are not in the class body. Their compartments are defined after the class declaration, as you will see soon.

The inverse clause is optional and it shows a bidirectional relationship to another attribute in the class of the attributes' type. In this case, JavaFX will automatically perform updates (insert, replace, and delete).

You can find a more complete tutorial on Java.net.

Playing Around with JavaFX

In this section, you will see a set of examples that covers a variety of JavaFX possibilities and particularities. The main purpose of these examples is to get you familiar with JavaFX code writing and with the logic of a JavaFX application. The second goal is to convince you that JavaFX deserves a closer look when you need to develop cool GUIs, animations, and nice effects with only a few lines of code. All the examples presented will introduce skills that are specific to JavaFX.

Every example is preceded by a short description, so don't expect line by line comments. All of these should be accessible enough to run yourself, so let's begin.

When you need to print the value of a variable/attribute with System.out.println you can place the name of it inside quoted strings, as shown in Listing 2:

Listing 2
//expressions within quoted text
import java.lang.System;
var mynumber:Number = 10;
System.out.println("Number is: {mynumber}");

Result: Number is: 10

JavaFX supports a useful facility known as the cardinality of the variable. This facility is implemented with the next three operators:

  • ?: Optional (may be null)
  • +: One or more
  • *: Zero or more
Listing 3
//cardinality of the variable
import java.lang.System;
var mynumbers:Number* = [1,2,7];
System.out.println("Numbers are: {mynumbers}");

Result: Numbers are: 1 2 7

In JavaFX, it is possible to not specify the type of a variable in its declaration. This will not be an error, because JavaFX will automatically find out the type of the variable from its use.

Listing 4
//the variable's type is optional
import java.lang.System;
var days = ["Monday,","Friday,","Sunday"];
System.out.println("You have to work: {days}");

Result: You have to work: Monday, Friday, Sunday

To get the size of an array, you can use the sizeof operator:

Listing 5
//getting the size of an array
import java.lang.System;
var lotto = [21,30,11,40,5,6];
System.out.println("Array size:{sizeof lotto}");

Result: Array size: 6

To get an array from a subarray that satisfies a condition, you can use the [] operator. The condition is placed between [] and is evaluated as a Boolean. This is similar with the XPath predicates.

Listing 6
//using the [] operator - similar to its use in XPath
import java.lang.System;
var mynumbers = [1,2,7,3,30,15,14,6,4];
var numbers = mynumbers[n|n < 10];
System.out.println("Numbers smaller that 10 are: {numbers}");

Result: Numbers smaller than 10 are: 1 2 7 3 6 4

To get the ordinal position of an element within an array, you can use the indexof operator:

Listing 7
//returning the ordinal position of an element within an array
import java.lang.System;
var mynumbers = [1,2,7,3,30,15,14,6,4];
var number_four = mynumbers[indexof . == 4];
System.out.println("Number four:{number_four}");

Result: Number four: 30

When you want to insert an element into an array you can use the insert statement and one of the following:

  • as first: for inserting into the first position
  • as last: for inserting into the last position (this is the default)
  • before: for inserting before a position
  • after: for inserting after a position

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow