Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Calling JavaFX From Java?

DZone's Guide to

Calling JavaFX From Java?

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}