Calling JavaFX Classes from Pure Java Code
Posted: under JavaFX Technology.
Tags: call, interoperability, invoke, Java, javafx
Just after the release of JavaFX 1.0, in one of my posts JavaFX and Java Interoperability, I discussed three possible approaches to invoke JavaFX features from the Java side. These approaches were:
|
1. The ScriptEngineManager class. It is based on JSR-223, the java scripting API, which allows a java program to call a script(such as JavaFX Script, javascript, perl). 2. The JavaFX reflection API. It can probably call any classes in JavaFX. 3. The JavaFX class implements a Java interface so that a Java program can invoke the JavaFX class via the interface. |
The third one seems the most elegant way to call JavaFX from Java. However, there is a drawback: the program should start from the JavaFX side. The reason is that it is simpler to use JavaFX code to instantiate the JavaFX classes which can be passed to Java code. Nevertheless, in some scenario, it would be better to start the program from the java side. For example, if you want to add in some JavaFX features to an existing large java application, it is better to have java code as the entry point. To solve this issue, I am combining the essence of Approach 2 and 3 to create the below example.
Let’s say we want to invoke the latest charting functions of JavaFX 1.2 from the java code. We will first use the JavaFX reflection API to instantiate the JavaFX class. We then use it via its java interface. So we define a Java interface first.
/*
* JavaInterface.java
*
* @author Henry Zhang http://www.javafxgame.com
*/
package javatest;
public interface JavaInterface {
public void addData(String name, float data);
public void showChart();
}
The next step is to create a JavaFX class MyChart to implements this interface:
/*
* MyChart.fx
*
* @author Henry Zhang http://www.javafxgame.com
*/
package javatest;
import javafx.scene.chart.PieChart;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart3D;
public class MyChart extends JavaInterface {
var chartData : PieChart.Data[] = [];
public override function addData( l:String, v: Number):Void {
var labelString = l;
var data = PieChart.Data {
label : l
value : v
action: function() {
println("{labelString} clicked!");
}
} ;
insert data into chartData;
}
public override function showChart() : Void {
var chart =
PieChart3D {
data : chartData
pieThickness: 25
pieLabelFont: Font{ size: 9 };
pieToLabelLineOneLength: 10
pieToLabelLineTwoLength : 20
pieLabelVisible: true
pieValueVisible: true
translateY: -50
};
Stage {
title: "PieChart Window"
width: 520
height: 300
scene: Scene {
content: [
Text {
font : Font {
size : 16
}
x: 200
y: 20
content: "Pie Chart"
},
chart
]
}
}
}
}
The last thing is to write the java main class JavaTest.
/*
* JavaTest.java
* @author Henry Zhang http://www.javafxgame.com
*/
package javatest;
import javafx.reflect.FXClassType;
import javafx.reflect.FXLocal;
import javafx.reflect.FXLocal.Context;
import javafx.reflect.FXLocal.ObjectValue;
public class JavaTest {
public static void main(String args[]) {
Context context = FXLocal.getContext();
FXClassType instance = context.findClass("javatest.MyChart");
ObjectValue obj = (ObjectValue)instance.newInstance();
JavaInterface ji = (JavaInterface)obj.asObject();
String [] labels = {"January", "Febuary", "March", "April"};
int [] values = { 18, 20, 25, 37 };
for ( int i=0; i < values.length; i++ ) {
ji.addData(labels[i], values[i]);
}
ji.showChart();
}
}
There are three lines in the above code to instantiate a JavaFX class via reflection:
Context context = FXLocal.getContext();
FXClassType instance = context.findClass("javatest.MyChart");
ObjectValue obj = (ObjectValue)instance.newInstance();
The next line is to convert the JavaFX instance into a java interface so that it can be used by Java side:
JavaInterface ji = (JavaInterface)obj.asObject();
If you are using NetBeans IDE, you can set javatest.JavaTest as the main class in your project properties. This makes sure that the entry point of your program is a java class. Build this project you will get a javatest.jar. Running this program produces the below screenshot:
To run it from the command line, use the below command:
javafx -jar javatest.jar
Actually, you could do it in the purest java style by including all the JavaFX runtime stuffs, the command would look like this:
java -Djava.library.path="<path to javafx sdk lib>"
-classpath "<all javafx sdk jars>" -jar javatest.jar
Since there are many jar files used by the JavaFX, this purest java approach turns out to be very troublesome. I would rather use the javafx command, which is a wrapper of the above java command.
Please leave comments if you have any questions.
This article is cross-posted at Pure Java Code to Call JavaFX Class. The Chinese translation can be found at http://www.javafxblogs.com.
P.S. Stephen Chin has released the JFXtras 0.5 version. In this project, a class SceneToJComponent is provided to load a JavaFX scence as a SWING component. Though it uses the internal method of the Scence class, it can insulate your code from future JavaFX change(i.e. Steve will take care of the changes for you.
)
Comments (4)
Jun 21 2009