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.
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.
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();
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.
Note
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.
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
.
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)); }
Note that in JavaFX, the extends
keyword is used to implement the interface instead of implements
as in Java.
Note that there are other ways to achieve integration between Java and JavaFX. The rules vary depending on the level of integration you are seeking:
1. Type integration using Java types from JavaFX, as shown in this recipe.
2. Framework integration for example, calling Swing components from JavaFX.
3. API integration wrapping native Java libraries within JavaFX classes to expose them as JavaFX components