How-To's > How do I combine JavaFX and Java?
Mix Java technology and JavaFX technology by calling a Java object inside of JavaFX Script with one or more of the following methods:
- Instantiate a Java package into a JavaFX file and call a Java class.
- Use a JavaFX class to extend a Java class, abstract class, or interface.
Some limitations are imposed on using JavaFX APIs that are supported in a specific profile. For example, when you are developing a JavaFX mobile profile, you must use Java Class Library files that are supported in the JavaFX Mobile runtime only. Similarly, you cannot use a desktop-only API in a JavaFX mobile application.
Instantiate a Java Package Into a JavaFX File
Here is an example that demonstrates how a Java class Surface3D.java is imported into a JavaFX application, and how the JavaFX Script calls a Java Class.

function getPerspectiveTransform(rotationDegree:Number,
x:Number, y:Number, w:Number, h:Number): PerspectiveTransform {
var rotationRadians: Number = Math.toRadians(rotationDegree);
var camera: Surface3D = new Surface3D();
camera.translate(x + (w / 2.0),y + (h / 2.0), 0);
camera.concat(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, distance, 0);
var view: Surface3D = new Surface3D();
view.scale(2 * w, 2 * h, 1);
view.translate(-0.5, -0.5, 2);
if (
rotateAxis == ROTATE_AROUND_X) {
scene.rotateYZaroundX(rotateOffset, 0, rotationRadians);
} else if(rotateAxis == ROTATE_AROUND_Y) {
scene.rotateXZaroundY(rotateOffset, 0, rotationRadians);
}
getPerspectiveTransform(view, camera, initialEffect);
}
Use a JavaFX Class to Extend a Java Interface
Here is an example of using a JavaFX class to extend a Java Class Library file. This capability is useful because it enables you to pass the JavaFX object into Java code:
/** Foo.java */
public interface Foo {
public abstract void doSomethingUseful();
}
/** Fizzle.fx */
public class Fizzle extends Foo {
override public function doSomethingUseful():Void {
myBankAcount.balance += 1000000
}
}
/** Somewhere in a java class that uses Foo */
void blah(Foo foo) {
if ( publicDebt < javafx.util.Math.pow(10, 12) ) foo.doSomethingUseful();
}
/** Somewhere in javafx */
var fizzle = Fizzle {}
blah(fizzle);
Examples
- Searching for Primes Under One Million
The following example uses Java packages and searches for all primes below one million and returns the number found. - Stock's Symbol-Based Quote and Ticker With Yahoo FEED
StockQuote is a simple JavaFX application that provides the current stock data based on the Yahoo Symbols. The results are provided in CSV files and are parsed by using simple Java string API. - JavaFX MediaBox Player for Streaming Video
The JavaFX MediaBox Component is a visual component that provides the standard video player controls. - JavaOne 2009 Hands-on Lab: Making a Java, Swing, JavaScript, and JavaFX Technology Smoothie
Last Updated: November 2009
[Return to How-To's Home]
Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of repliesyour information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.