Some questions that were here have been moved to the Talk page.

Introduction Edit

There are some rough spots in the Java-to-JavaFX integration. Generally, JavaFX prefers to be in complete control, creating objects and managing them. This article with show a couple of ways of interacting with Java objects. For more details, see Combining data models from regular Java classes and Talk:Combining data models from regular Java classes.

Giving objects to JavaFX Edit

It is relatively simple for Java to provide data to a JavaFX file as described in Programmatically invoking a JavaFX Script. By setting named values in the Bindings object, JavaFX can access them. Unfortunately, there is no semblance of compile-time safety in this case, as the JavaFX code has no concept of the JSR-223 code invoking it.

The above link contains an example of giving a Java Date object to JavaFX code which then uses it.

Creating Java objects Edit

You've actually already done this. If you've run any of the examples of JavaFX, you've already created Java objects. More specifically, the JavaFX core classes (in javafx.ui.* and beyond...) are creating Java instances all the time to do their work, whether to make JButtons or gradients. You can delve into any of the core .fx files by looking in the JavaFX distribution zips or jars.

To create an object, just import it and instantiate it as usual.

var aTable = new <<javax.swing.JTable>>();

(See the FAQ if the above syntax is new to you.)

You can make method calls, such as setters and getters, exactly as you would in Java. If JavaFX gets a bit confused, it sometimes helps to specify the type of the variable explicitly:

import javax.swing.JTable;
var aTable:JTable = new JTable();

Note: to use a custom Swing component in your JavaFX code, be sure to read Use any swing component.

Implementing Interfaces Edit

This is the best way to perform Java to JavaFX integration if your main script is in JavaFX and you have Java code that needs to modify the properties of the JavaFX class.

First, create the interface you want to use. For example, here is

 /* */
 public interface SayHello {
  public void showMessage(String s);

Then implement it. Here I show sample JavaFX code in the file SayHelloImpl.fx

 /* SayHelloImpl.fx */
 public class SayHelloImpl extends SayHello {
   override function sayHello(s:String) : Void {
     //do something

The disadvantage of this method is that SayHelloImpl objects can only be created in JavaFX code.

Extending Abstract Classes Edit

Community content is available under CC-BY-SA unless otherwise noted.