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

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

If you searched for a solution to embed JavaFX components in Swing you won’t have success with a version which is officially supported.

But as always there are workarounds like the one from Josh Marinacci. So far I can’t get it working it gives me:

java.lang.NoSuchMethodException: MyScene.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)

The simple workaroung is now the following (Java Side)

JXScene scene = new JXScene();
scene.setScene(new de.timefinder.planner.widgets.SwingHook());
// ... or create the class indirectly
// Class clazz = Class.forName("de.timefinder.planner.widgets.SwingHook");
// scene.setScene(clazz.newInstance());

// now copy parts of the code from Josh's article
class JXScene extends JComponent {
    public void setScene(Object scene) {
        String helperName = "com.sun.javafx.scene.JSGPanelSceneImpl";
        FXClassType type = FXContext.getInstance().findClass(helperName);
        FXObjectValue panelSceneImpl = type.allocate();
        panelSceneImpl.initVar("scene", FXLocal.getContext().mirrorOf(scene));
        panelSceneImpl.initialize();

        FXValue jsgPanelV = type.getVariable("jsgPanel").getValue(panelSceneImpl);
        JComponent jsgPanel = (JComponent) ((FXLocal.ObjectValue) jsgPanelV).asObject();
        add(jsgPanel, BorderLayout.CENTER);
    }

    public JXScene() {
        setLayout(new BorderLayout());
    }
}

And use the same JavaFX side snippet:

public class SwingHook extends Scene {
    init {
        content = MainWindowContents{}.getContents()
    }
}

From http://karussell.wordpress.com/

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

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 }}