Java Reference
In-Depth Information
var dim = java.awt.Dimension {
width: 500
height: 600
};
println(dim);
prints: java.awt.Dimension[width=0,height=0]
To create a JavaFX instance from a Java object, use the object literal syntax with
open and close curly braces. For example, the following script will print the date
24 hours from now.
var millis = java.lang.System.currentTimeMillis() +
24 * 60 * 60 * 1000;
var date = java.util.Date { };
date.setTime(millis);
println(date);
Although you cannot initialize the Java object attributes, it is possible to define
abstract function implementations within an object literal. The first example
shows how to override the methods in a Java interface with JavaFX functions.
For example, the Comparator interface's Java functions are
public int compare(Object o1, Object o2);
public boolean equals(Object obj);
In JavaFX, these can be implemented within the object literal syntax:
var comp = java.util.Comparator {
override function compare(o1:Object, o2:Object) :
Integer {
if(o1 instanceof java.lang.Comparable and
o2 instanceof java.lang.Comparable) {
(o1 as java.lang.Comparable).compareTo(
o2 as java.lang.Comparable);
}else { -1; }
}
override function equals(obj:Object) : Boolean {
this == obj;
}
};
This next example shows how to override a method in a Java class. This over-
rides the Java method:
Search WWH ::




Custom Search