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

When you want to delete an element from an array, you can use the delete statement.

Listing 8
//insert and delete statement
import java.lang.System;
var mynumbers = [1,2,7];
System.out.println("Before inserting anything:
    {mynumbers}");
insert 10 into mynumbers;
System.out.println("After inserting at the end
    the \"10\" value:{mynumbers}");
insert [8,6,90] as first into mynumbers;
System.out.println("After inserting at the first
    positions the \"8,6,90\" values:{mynumbers}");
insert 122 as last into mynumbers;
System.out.println("After inserting at the
    end the \"122\" value:{mynumbers}");
insert 78 before mynumbers[3];
insert 11 after mynumbers[3];
System.out.println("After inserting the \"78\"
    and \"11\" values before/after the 3rd
    element:{mynumbers}");
delete mynumbers[. == 122];
System.out.println("After deleting:{mynumbers}");

Result:
Before inserting anything: 1 2 7
After inserting the 10 value at the end: 1 2 7 10
After inserting the 8, 6, and 90 values at the first positions: 8 6 90 1 2 7 10
After inserting the 122 value at the end: 8 6 90 1 2 7 10 122
After inserting the 78 and 11 values before/after the 3rd element: 8 6 90 78 11 1 2 7 10 122
After deleting: 8 6 90 78 11 1 2 7 10

A great facility of JavaFX is its list comprehensions. This facility is implemented by the select and foreach operators. Here are two examples (one with select and one with foreach) for getting the odd numbers from an interval.

Listing 9
//JavaFX select and foreach operators
import java.lang.System;
function odd(p:Number) {
    return select i from i in [1.0 ..p]
        where (i%2 == 0.0);
    }
var result = odd(10.0);
System.out.println("Odd numbers:{result}");

Result: Odd numbers: 2.0 4.0 6.0 8.0 10.0

Listing 10 (same as Listing 9, but with foreach)
//JavaFX select and foreach operators
import java.lang.System;
function odd(p:Number) {
    return foreach (i in [1.0 ..p] where (i%2 == 0.0)) i;
    }
var result = odd(10.0);
System.out.println("Odd numbers:{result}");

This example illustrates that foreach can be very useful for creating nice effects.

Listing 11
//JavaFX select and foreach operators
import java.lang.*;
import javafx.ui.*;
import javafx.ui.canvas.*;
Frame {
    centerOnScreen: true
    visible: true
    height: 500
    width: 500
    title: "Foreach demo..."
    onClose: operation() {System.exit(0);}
    content: ScrollPane {
    background: white
    view: Canvas {
        content: bind foreach (i in [1..8], j in [1..8])
        Rect {
        x: i*30
        y: j*30
        width:30
        height:30
        fill: Color {red: (100+i) green: (100+j) blue: (100+(i*j))}
        stroke:white
        strokeWidth:1
        }
    }
    }
}

Running listing 11
Figure 4. Running Listing 11

When you need to use a variable or attribute that has the same name as a JavaFX keyword you must insert that name between double angle brackets, like this:

Listing 12
//Identifier quotes
import java.lang.System;
for (<<for>> in [0..3]) {
    System.out.println("for = {<<for>>}");
    }

Result: for = 0   for = 1   for = 2    for = 3

JavaFX can be a great tool when you need to develop Swing interfaces, because JavaFX considerably reduces the amount of code and cooperates well with the javax.swing.* packages. In an earlier section (Testing the Hello World application with JavaFX plug-in for NetBeans 5.5), you saw how easy it is to create a simple frame. Here are two more examples, creating a Button and a TextField:

Listing 13
import javafx.ui.*;
import java.lang.System;
Frame{
    content: Button {
    text: "Exit"
    action: operation() {
    System.exit(0);
    }
    }
    visible: true
}

 Running listing 13
Figure 5. Running Listing 13

Listing 14
import javafx.ui.*;
Frame {
    content: GroupPanel {
    var myRow = Row { alignment: BASELINE }
    var label_col = Column { alignment: TRAILING }
    var field_col = Column { alignment: LEADING  }
    rows: [myRow]
    columns: [label_col, field_col]
    content:
    [SimpleLabel {
    row: myRow
    column: label_col
    text: "Type your text here:"
    },
    TextField {
    row: myRow
    column: field_col
    columns: 50
    }]
    }
    visible: true
};

Running listing 14
Figure 6. Running Listing 14

Java.net also has a tutorial on creating Swing interfaces with JavaFX.

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

Next Pagearrow