Java Reference
In-Depth Information
How it works...
Java classes and JavaFX classes are binary-compatible. When you compile your JavaFX
classes, the JavaFX compiler creates a Java class file (a.class extension file). There are
three points that should be made regarding the code snippet in this recipe:
1. Similar to Java, JavaFX script supports the new operator when creating a new object
instance. This makes it easy to instantiate objects written in Java from within JavaFX.
While JavaFX objects can be instantiated using Object Literal Notation
and the new operator, Java objects can only be instantiated with the
new operator.
2. The type inference engine will automatically determine the type of the assignment
using the Java object's type.
3. Once you have access to the Java object instance, you may invoke any public
members on that object.
There is more...
In JavaFX, not only can you instantiate pure Java classes, you can also implement Java
interfaces directly. Using this mechanism, you can achieve two-way integration between
Java and JavaFX. Again, the full listing of the code presented here can be found in package
ch01/source-code/src/java .
Implementing a Java interface in JavaFX
The steps required to implement a Java interface in JavaFX are simple. You first create
a JavaFX class which extends the interface. Then, you provide JavaFX functions which
implement methods defined in the interface, as given the following Java interface:
interface JavaInterface {
int add(int num1, int num2);
}
You can create JavaFX script with the following implementation:
public class JavaInterfaceImpl extends JavaInterface {
override function add(num1, num2) {
num1 + num2;
}
}
public function run() {
var adder = JavaInterfaceImpl { }
println(adder.add(1, 2));
}
 
Search WWH ::




Custom Search