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

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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/

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}