Open Source JavaFX Now!

JFXtras Core Logo

JXScene

A Java class that allows embedding a javafx scene in java/swing. It allows communication between the swing and javafx side.

This class was originally created by Richard Bair and Jasper Potts and re-implemented for version javafx1.2 For more examples and documentation check out this blog post: http://blogs.sun.com/javafx/entry/how_to_use_javafx_in This implementation brings up some minor additions not present on the original JXScene.

Differences from the original JXScene

  • On this JXScene through the use of generics you can specify which interface the javafx scene class implements, avoiding the extra casting required on the previous version. Note that you can still make the javafx scene class implement other interfaces and cast the java scene object to those interfaces on the java side.
  • There is a new method that allows you to initialize the javafx scene object's public variables:
public void setScene(String sceneClassName, Map<String, Object> initVars)
Just pass in a map containing the <name of variable> - <value> pairs.

Example

JavaFX Object, extends Scene and implements Talker (JavaInterface).
public class YourJavaFXScene extends Scene, Talker {
   public override function talk(msg:String):Void {...}
}

Java side

public class TheFrame extends javax.swing.JFrame {
   public TheFrame() {
      final JXScene<Talker> jxScene = new JXScene<Talker>();
      jxScene.setScene("com.yourproject.YourJavaFXScene"); // the name of your main JavaFX class
      add(jxScene); // add the scene your swing scene
      com.sun.javafx.runtime.Entry.deferAction( new Runnable() {
         public void run() {
            jxScene.getScene().talk("Hi There");//generics in action.
         }
       });
    }
 
   public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            new TheFrame().setVisible(true);
         }
      });
   }
}

Requirements

  • To use this class all the jar files in the JavaFX SDK lib/shared and lib/desktop must be included in the java CLASSPATH.
  • If you want to use javafx video you must reference the jmc.dll. You can do this with the following arguments:
java -Djava.library.path=<path to jmc.dll file>

Note however that we were unable to reproduce video on vista and windows 7 - the jvm crashes without giving any error information. But it does work on windows xp.

Final Notes

Embedding Javafx in a swing app is not currently officially supported.The next release of JavaFX will make this much harder to accomplish as it will no longer be dependent on AWT/Swing, the underlying graphics engine will be replaced.

However there are plans to officially support embedding javafx in a swing app and the next release codenamed Soma will have some pre requisite work done on that matter.

It seems that javafx video doesn't work with this approach on windows 7 and windows vista. On windows XP it works fine.

0 Attachments 0 Attachments
1495 Views