Calling JavaFX from Java

For my open source timetabler I had to searched for a solution to embed JavaFX components into Swing. But I didn’t see 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 workaround 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()
    }
}

Important Update: Pedro commented on Amy Fowler’s Blog that you have to have a run method in the Scene class:

public function run(args: String[]) {
 AnimationScene = AnimationScene { }  // just an example
 }

Then my fix to Josh’s workaroung shouldn’t be necessary (I didn’t tried it so far, because this fix would not work for javafx1.2)

Advertisement

6 thoughts on “Calling JavaFX from Java

  1. We can define a interface in Java. JavaFX class can implement this interface. Now Java can call JavaFX class functions.
    This is the standard documented way of calling JavaFX from Java. I have done this on an open source project http://timefinder.sourceforge.net/
    Anyone can download the code and check for themselves.
    Vijay.

  2. Hi Vijay, nice to meet you here 🙂

    Yes, your solution would be nice, if we could do the same for embedding JavaFX components into Java. Any working snippet?

    I now understand what you meant with hack at the very beginning of our conversation – and now I can totally agree that this is a hack, but I didn’t find a better working solution.

    Peter.

  3. Pingback: JavaFX and Maven « Java and more …

  4. Hi
    i’m beginer in java FX, i’ve tried to create the parser function to get data from Xml file i got the same error message:

    java.lang.NoSuchMethodException: newpackage.NewJavaFXClass.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)
    at java.lang.Class.getMethod(Unknown Source)
    at com.sun.javafx.runtime.Entry.start(Entry.java:63)
    at com.sun.javafx.runtime.Main.main(Main.java:80)

    i’m trying to get the data from the XML file and save them into a public variable so i can use them in other classes.
    bellow is the function that i’m using:
    import javafx.data.pull.*;
    import java.io.FileInputStream;
    import java.lang.System;
    import javafx.data.xml.QName;

    public function loadData( ): Void{
    var imageURL8 : String [] = [ “” ];
    var myparser = PullParser {
    documentType: PullParser.XML;
    onEvent: function (e: Event) {
    // starter xml elements
    if(e.type == PullParser.START_ELEMENT){
    // Carousel
    if(e.qname.name.equals(“Carousel”)){
    System.out.println(“Carousel”);
    }

    // PrincipalService
    if(e.qname.name.equals(“PrincipalService”)){
    System.out.println(“PrincipalService”);
    }

    // Image
    if(e.qname.name.equals(“Image”)){
    System.out.println(“Image”);
    }

    // Label
    if(e.qname.name.equals(“Label”)){
    System.out.println(“Label”);
    }

    // ImageLink
    if(e.qname.name.equals(“ImageLink”)){
    System.out.println(“ImageLink”);
    }
    for (i in [1..5]){
    if(e.qname.name.equals(“Url{i}”)){
    imageURL8[i]= e.getAttributeValue(QName{name:”Address”});
    System.out.println(imageURL8[i]);
    }
    }

    // description
    if(e.qname.name.equals(“description”)){
    var type= e.getAttributeValue(QName{name:”type”});
    System.out.println(type);
    if(type.equals(“long”)){
    System.out.println(“ok”);

    } else {

    }
    }
    }

    // ending xml elements
    if(e.type == PullParser.END_ELEMENT){
    if(e.qname.name.equals(“data”)){

    }
    }
    }
    input: new FileInputStream(“/Parametter.xml”);
    }
    myparser.parse();

    }

    Can any body help me to solve this problem as soon as possible.

    other thing that’s not related to this, but it’s closer, how can we save data from JavaFX to an XML file

    (i’m using Netbeans 6.9 with JavaFX 1.3)
    thank you in advance for your help

Comments are closed.