Java Reference
In-Depth Information
Integrating your JavaFX code with Java
JavaFX is built directly on top of the Java-Virtual-Machine (JVM). Therefore, your JavaFX code
has access to the entire Java ecosystem including all of the standard Java libraries such as
IO, JDBC, XML, Swing, and so on. Any compiled Java code accessible on the class path can
be called from within a JavaFX script. This recipe covers the techniques required to integrate
JavaFX and Java code together.
Getting ready
This section explore integration techniques between JavaFX and Java. You should have
familiarity with the Java language, its libraries, or have the ability to create your own classes
or libraries to be called from JavaFX.
How to do it...
The easiest way to see Java and JavaFX interoperate is to create an instance of a Java object
and invoke a method on the instance from within JavaFX. Let's go through an example. You
can see the full code listing in package ch01/source-code/src/java .
First create and compile this simple class:
public class JavaObject {
private String name;
public JavaObject(String n){
name = n;
}
public void printReverse() {
for(int i = name.length()-1; i >= 0; i--){
System.out.print (name.charAt(i));
}
System.out.println();
}
}
Now create a JavaFX script which creates an instance of JavaObject and invoke the the
printReverse() method on the class.
var javaObject = new JavaObject ("Hello World!");
javaObject.printReverse();
 
Search WWH ::




Custom Search