How-To's > How do I combine JavaFX and Java?

Version: JavaFX 1.3

Mix Java technology and JavaFX technology by calling a Java object inside of JavaFX Script with one or more of the following methods:

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.

NetBeans project window that shows a Java class added to a JavaFX application.

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

Last Updated: April 2010
[Return to How-To's Home]