Over a million developers have joined DZone.

Calling JavaFX From Java?

· Java Zone

Learn more about how the Java language, tools and frameworks have been the foundation of countless enterprise systems, brought to you in partnership with Salesforce.

While experimenting with JavaFX, remember that Java is never far away.

In this case, let's call out to Jim Weaver's JavaFX Calculator demo:

Here we go, here's all that's needed, via the Scripting API, which is included in the JavaFX SDK:

package calc;

import java.io.InputStreamReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class CalculatorLauncher {

public static void main(String[] args) {
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("fx");
InputStreamReader reader = new InputStreamReader(CalculatorLauncher.class.getResourceAsStream("Calculator.fx"));
engine.eval(reader);
} catch (ScriptException ex) {
}
}

}
And so, here's my whole application, calling Jim Weaver's Calculator demo:

Nice. Even better would be to be able to embed that JavaFX Stage (or a panel-like part thereof?) into a JFrame. Then one would have the best of both worlds: the graphic "oomph" of JavaFX, together with the daily low level grunt work of Java. That, I believe (and hope), is the promise of JavaFX for Java developers (and Groovy developers), as opposed to designers and similar graphic artists, who seem to be the primary target of JavaFX.

Discover how the Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services, brought to you in partnership with Salesforce.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}