How to Use JavaFX in Your Swing Application

JavaFX has been out for a couple of weeks now and the response has been great. Clearly there are bugs to work on and features to add, but the core is powerful and ready to go. One feature I've gotten lots of requests for is the ability to mix Swing with JavaFX code and graphics. The JavaFX samples page has an example of how to embed an existing Swing component or panel in your JavaFX application. However, we don't have an example of how to do the reverse: embed a JavaFX scene in your Swing application. I asked Rich and Jasper about this and they had the same need as well. After a long train ride to Devoxx this is the code they came up with.

First things first: What I'm about to show you is a hack. It is completely unsupported, uses private APIs, and almost certainly will break in the next version. However, if we see a lot of interest we may add a permanent supported API in a future release of JavaFX, so let us know what you think of it. Second, this is a hack. Most likely not all features of JavaFX will work in this scenario and we haven't done any more than trivial testing. Don't go building a production application using this technique. All that said, on with the show:

The JXScene class

The core of this hack is the JXScene class, a Swing component which knows how to draw JavaFX scene graph nodes. It uses reflection to instantiate your JavaFX scene class and then pull out the internal canvas object.

package org.jdesktop.swingx;

import com.sun.javafx.runtime.Entry;
import com.sun.javafx.runtime.TypeInfo;
import com.sun.javafx.runtime.sequence.Sequence;
import com.sun.javafx.runtime.sequence.Sequences;
import java.awt.BorderLayout;
import java.lang.reflect.Method;
import javax.swing.JComponent;
import javafx.reflect.*;

 * @author Richard Bair and Jasper Potts
public class JXScene extends JComponent {
    private String script;
    private Object scriptObject;

    // fully qualified path to script that creates a Scene
    public void setScript(String path) {
        this.script = path;
        try {
            Class app = Class.forName(path);
            final Method main = app.getMethod(Entry.entryMethodName(), Sequence.class);
            Object o = main.invoke(null, Sequences.make(TypeInfo.String, new String[0]));

            String helperName = "com.sun.javafx.scene.JSGPanelSceneImpl";
            FXClassType type = FXContext.getInstance().findClass(helperName);
            FXObjectValue panelSceneImpl = type.allocate();
            panelSceneImpl.initVar("scene", FXLocal.getContext().mirrorOf(o));

            FXValue jsgPanelV = type.getVariable("jsgPanel").getValue(panelSceneImpl);
            JComponent jsgPanel = (JComponent)((FXLocal.ObjectValue) jsgPanelV).asObject();
            add(jsgPanel, BorderLayout.CENTER);
            scriptObject = o;
        } catch (Exception e) {

    public Object getScriptObject() {
        return scriptObject;
    public String getScript() {
        return script;
    public JXScene() {
        setLayout(new BorderLayout());

Setting up a Swing / JavaFX application

With the JXScene class in your arsenal, here's the steps to use it:

  • download the jar of JXScene or compile it on your own from the code above
  • Create a project in NetBeans for the JavaFX part of your application. Put your JavaFX Script code in there.
  • Make your main JavaFX class extend Scene like this:
    public class MyScene extends Scene {
        init {
            content = [
                Rectangle { width: 200 height: 200 fill: Color.BLUE },
                Text { x: 20 y: 20 content: "Greetings Earthling!" fill: Color.WHITE }
  • Create a Java or Desktop Java application in NetBeans for the Java / Swing part of your application. Put your Swing code in there.
  • Add this code to your Swing app:
    JXScene scene = new JXScene(); // create a new JXScene
    scene.setScript("com.sun.test.MyScene"); // the name of your main JavaFX class
    mainPanel.add(scene); // add the scene your swing scene
  • In your Java project go to the Project Properties dialog and the Libraries tab.
  • Add the JXScene project or jar to the classpath of the Java project
  • Add the JavaFX project to the classpath of the Java project
  • Download the JavaFX standalone (commandline) SDK from here.
  • Add all of the jars in the lib/desktop and lib/shared directories of the JavaFX SDK to the classpath of the Java project
  • Compile your JavaFX project
  • Compile and run your Java project

If everything went well you will get a screen that looks like this:

The menu and frame and bottom status bar are from my Swing application, but the center panel is my JavaFX scene.


That's all there is to it for working locally, but what about when you want to deploy your application? The JavaFX runtime is always deployed as a shared cached JNLP extension, so to use it from your own deployed webstart application just add the extension to the resources section of your JNLP.

in TestJXScene.jnlp

<extension name="JavaFX Runtime" href="" />

Interaction between JavaFX and Java

For any non-trivial application you are going to want to have some interaction between the Java and JavaFX sides of your application. In general you should try to call Java code from the JavaFX side, not the other way around. While it is possible to invoke the various JavaFX methods using reflection, this is cumbersome and not recommended. Instead, make your JavaFX code implement a Java interface. Then the Java side can cast your JavaFX objects to that interface and invoke your functions as if they were Java methods. For example, the JavaFX scene above could be declared to extend the java.awt.event.ActionListener like this:

in com.sun.test.MyScene.fx

public class MyScene extends Scene, ActionListener {
    override public function actionPerformed(e:ActionEvent)  {
        println("this is the callback");

    init {
	// more code

Now the Java code can cast the JavaFX object to an ActionListener and invoke the actionPerformed method like this:

JXScene scene = new JXScene();
ActionListener al = (ActionListener) scene.getScriptObject();


Swing and JavaFX interaction is clearly possible but not as clean as we would like. If you have any suggestions for what you'd like to do with Swing/JavaFX mixing and what the ideal API would be, please let us know. We always appreciate your feedback.


Thanks for the information. If you have a Node instance, I believe you can do something like this:

Node node = (Node) fxObject;
FXNode fxNode = node.get$javafx$scene$Node$fxNode();
JSGPanel panel = new JSGPanel();
// JSGPanel is a Swing component

JavaFX GUI classes are supposed to be declared as Stage's, right? In that case calling the entry method works, but a new JFrame will be opened. There appears to be a hack to make applets work with Stage's.

Posted by Jose on December 19, 2008 at 09:50 AM PST #

Yes, yes Interest. All the Java programmers I showed JavaFX so far had that shiny christmas look in their eyes untill I told them it's either 100% JavaFX or bust. I also had that idea of spicing up existing apps with ab nice fx based Accordion bar or replace some of my svg stuff with JavaFX; But i was very disappointed to find no JFXPane with a setScript() method.

I think JavaFX will get more (deserved) attention if it can blend into existing Swing applications.

Posted by Tante Wildrudt on December 20, 2008 at 01:55 AM PST #

Jose: yes that will work, but keep in mind that the name mangling will change in the future so this code is very likely to break in the next release.

Tante: does the code above do what you need?


Posted by Josh Marinacci on December 20, 2008 at 02:38 AM PST #

I trying to load a mediacomponent into a swing app but the video doesnt load :(

Posted by Clovis Teixeira on December 20, 2008 at 05:07 AM PST #

Is there interest? YES, to be able to extent my application that is based on Netbeans RCP, with visually appealing components (JPanels), e.g. interactive plots or charts. Right now, for my needs, i see in JavaFX more value at developing well-designed components (interactive data visualization), which can be integrated in Swing based apps. That means that the data model has to be set from the java-side, i think.

So, the integration wasn't designed from the beginning. Why? One question: Are we able to replace some day Netbeans RCP (Swing) with an equivalent platform based on JavaFX?

Posted by ilhami visne on December 20, 2008 at 02:02 PM PST #

Yes, I'd would like to have something like "JFXComponent" as part of [supported] JavaFX public API. The main usage of it is to include vector graphics and vector animations into otherwise Swing applications. Icons, logos, custom animated components -- those are all very good uses and have a potential to greatly simply Swing programmer's life.

Right now, when designers produce a UI drawing in Photoshop or Illustrator, a poor Swing programmer has to meticulously translate it all into Java2D code. But you can now convert a drawing into FXD file. The only missing piece is public API to wrap this FXD file into JComponent.

Posted by Roman Elizarov on December 20, 2008 at 07:14 PM PST #

Currently, there are many kinds of hacks for interactions between Java and JavaFX. This is enough to prove that a more convenient way is needed in future version of JavaFX. Just give two examples here:

1) I found it hard for JavaFX to make use of java classes like Hashtable, Vector etc. We can call hashtable.put(key, javaFXObject) without a problem, but it is impossible to call hashtable.get(key) because there is no type casting in JavaFX. I was trying to write a Hashtable wrapper in Java, but Java does not recognize JavaFX classes. Need something helpful here in javaFX.

2) If Java can invoke javaFX classes seamlessly, I think SWING may probably be replaced by javaFX at all. So, future Java GUI programming can be java+javaFX.

Posted by JavaFX Game on December 20, 2008 at 11:44 PM PST #

I'll join the chorus of people saying "YES!", please make it possible to embed JavaFX components in Swing apps. As others have said, I'd love to extend my apps with cool JavaFX graphics, but if it means I have to throw out all my existing Swing components that work just fine (including all those in SwingX, JGoodies, JIDE, etc), no thanks. It boggles my mind that JavaFX was built on the JVM and this wasn't one of the first things implemented. Make it easy to transition existing apps to JavaFX without starting from scratch. I hope that I'm just missing something here.

p.s. Is it really true as JavaFX Game notes that you can't call hashtable.get() from JavaFX? There are now several languages implemented on the JVM and this is not a limitation I'm familiar with in any of them.

Posted by Dave Ray on December 21, 2008 at 09:24 AM PST #

I again and again wonder how product definition works at SUN!!

You invent new features and capabilities (just to take the media support) and now you question whether there is interest to integrate this with existing products???

I would put it the other way around: if you don't offer a sensible (i.h. easy to use + supported) migration/integration path then all you will see are a lot of demo apps and here and there some little applet but nothing which will justify your huge efforts!

Posted by Product definition on December 21, 2008 at 04:35 PM PST #

I could start using JavaFX today in my day job if there was an officially sanctioned way of including JavaFX scenes in Swing applications. As it is, I'm resigned to playing about with it in my own time until Sun makes up it's mind about who it wants to pitch this to. At the moment it appears they are going all out for graphic designers and ignoring existing Java Swing programmers. If they expect Swing programmers to migrate over to JavaFX, they can't afford to ignore them for too long!

P.S. AFAIK you can cast in JavaFX using the 'as' keyword e.g. myHashtable.get("key") as MyClass

Posted by Craig Aspinall on December 22, 2008 at 12:26 AM PST #

At least for me, having this integration is very important. I've been trying to test a JavaFX UI using a test case written in Java.

I'd like to see an official API in the future. For now, I'll use the provided jar.

What is the license of the JXScene? I'm planning to use it with an Apache 2.0-licensed project.


Posted by Alex Ruiz on December 22, 2008 at 08:23 AM PST #

@Alex the JXScene class is provided as example code. You can do whatever you want with it.

Posted by Josh Marinacci on December 23, 2008 at 03:03 AM PST #

Cooool! Thanks Josh :)


Posted by Alex Ruiz on December 23, 2008 at 03:21 AM PST #

I'm having trouble getting this to work, I'm getting this exception trying to lookup a method javafx$run.

java.lang.NoSuchMethodException: com.sun.test.MyScene.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)
at java.lang.Class.getMethod(
at org.jdesktop.swingx.JXScene.setScript(
at com.vas.test.SwingFx.main(

Any ideas?

Posted by David MacGibbon on December 26, 2008 at 12:42 PM PST #

Echo Craig's comment. I've been assuming all along that since SUN has such a giant base of Swing developers that you would leverage that community with this new project. This makes me disappointed that SUN has diverted resources from the 3D API, something that's actually useful to me, to JavaFX, something that has turned out not to be.

Posted by Pete Christensen on December 27, 2008 at 11:18 PM PST #

Add my vote for an official API for generalized access of JavaFX classes from Java. I would really love if this took the simplest-for-the-developer form of:

import my.fx.package.Foo;

I understand that this may pose difficulties in some situations, but if this could work for at least some basic set of use cases life would be easier for some of us.

Posted by Chris Jensen on December 28, 2008 at 07:15 AM PST #

YES, YES, YES. Full interoperability between Java and JavaFX. I'd like to see the ability to

1) embed JavaFX visuals in a Swing app.
2) *easily* use Java classes in JavaFX without the funky notation
and (my favorite)
3) code classes in JavaFX (to take advantage of the neat syntax and binding) and use them in a full-strength Java app!

Posted by Ari G. on December 29, 2008 at 12:47 AM PST #

@David MacGibbon what code are you invoking? the com.sun.test.MyScene.javafx$run$ part doesn't look right.

Posted by Josh Marinacci on December 29, 2008 at 12:52 AM PST #

To help troubleshoot this, I have added println statements to the JXScene setScript method;

System.out.println("path=" + path);
Class app = Class.forName(path);
System.out.println("app=" + app);
String entryName = Entry.entryMethodName();
System.out.println("entryName=" + entryName);
final Method main = app.getMethod(entryName, Sequence.class);
System.out.println("main=" + main);

This is what I get on the console.

app=class com.sun.test.MyScene
java.lang.NoSuchMethodException: com.sun.test.MyScene.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)
at java.lang.Class.getMethod(
at org.jdesktop.swingx.JXScene.setScript(
at com.vas.test.SwingFx.main(

Posted by David MacGibbon on December 29, 2008 at 06:22 AM PST #

I can't get this to work in either Netbeans 6.5 or with the Eclipse plugin. I must be using the wrong version of something.

Would it be possible to post the complete source code ready to run?

Posted by David MacGibbon on January 02, 2009 at 12:27 AM PST #

YES YES! I love programming swing applications, but the lack of multimedia support is becoming a problem. I've tried java media framework, but support for it seems to be lacking.
JavaFX promises so much - lack of swing app implementation seems like a crime! I'm primarily interested in good video playing support within a swing application.


Posted by Joseph Obernberger on January 03, 2009 at 08:29 AM PST #

I would also like to see a bridge to JavaFX from Swing: this would surely ease some migration issues as one could start from a (most likely) already existing Swing application and could incorporate JavaFX where deemed appropriately.

Posted by Aloys Oberthuer on January 04, 2009 at 10:08 PM PST #

"However, if we see a lot of interest we may add a permanent supported API in a future release of JavaFX, so let us know what you think of it."

*may* ?? You've got to be joking. You MUST! It's unfortunate that it wasn't in the first release.

Posted by SWPalmer on January 04, 2009 at 11:55 PM PST #

+1 on a supported public API for this. I'll refrain from adding my surprise that this wasn't planned from the beginning.

Posted by Xendren on January 05, 2009 at 12:27 AM PST #

Josh, really, really important to get a feature like this built into javafx. Not just for swing, but for any java code (mobile msa, tv, desktop) needing to drive gui's, using any javafx gui library - not just swing. Is it worth while starting a thread somewhere on ideas for an api?

Posted by Nick on January 05, 2009 at 01:07 AM PST #

+1 on a supported public API for this

I will play with this hack soon. I think you totally need to make this work. Swing is not bound to disappear for a long time I think, too many apps are using it now. And despite all critics swing is cool and really really powerful ;-)

But being able to mix regular swing with JavaFX nodes with swing really adds a new world of possibilities for Java itself.

Posted by Herv&eacute; on January 05, 2009 at 07:45 AM PST #

Sorry. Replace: "you totally need to make this work" by "you totally need to support this officially"

Posted by Herv&eacute; on January 05, 2009 at 07:46 AM PST #

+1 please add official support. "The only strategy in getting people to switch to your product is to eliminate barriers."( You did that quite well for designers, please do the same for Swing developers.

Posted by Anton Epple on January 05, 2009 at 05:12 PM PST #

I am already in trouble, 'cause a couple of weeks ago I gave a promise to my
leader that the my new JavaFX app will run on goody old swing, hah! it's January now, and I'm still having problems with that! Happy new year to the folks in Sun!

Posted by east europian on January 05, 2009 at 05:35 PM PST #

I have to join the choir: Yes! In my opinion as well, this is a must. I can't see any adoption of JavaFX tech in commercial products I will participate in, in the forseeable future at least, without such an API. And, I am surprised as well that this isn't at least already part of the roadmap. We have invested a lot in Swing applications, and it would be a shame if we couldn't leverage any of the really cool stuff that JavaFX brings to this platform.

I think the larger part of the Java community is in fact in dire need of an adoption path.

Posted by Hans-Gunnar Vold on January 05, 2009 at 05:38 PM PST #

Good Sample, i think JavaFX can make it's way easier and faster if this kind of thing stops being a hack (even being so cool) and starts being some seriously supported feature, i know, i wanted to do some javafx based improvements to a production application i have, unfortunately, i had to drop the javafx approach for now. Can't wait 'till this is official!

Posted by Carlos Mario Alvarez Palacio on January 06, 2009 at 01:29 AM PST #

+5 ( team of 5 ) for general effort in Swing/JX integration. Like many other inhouse enterprise developers we have a large number of swing apps, with custom components that have been built up over the years for our particular industry ( Biotech ); the ability to mix and match is really important for adoption - really can't justify rewriting all the apps in JavaFX - would like to be able to enhance existing apps as and when.

So I'm interested in ways of enhancing existing apps with new components - I'd like to have a choice between those being written in JavaFXScript or coded in Java and accessing scenegraph and animation libraries directly from Java.

This is the reason that projects like JWebPane are also exciting - the ability to mix and match the best of different technologies (eg web and desktop ) is a really useful feature.

Posted by Enterprise Java developer on January 06, 2009 at 04:59 AM PST #

At our last JUG meeting, Giampaolo Melis introduced an alternative approach based on JDK Dynamic Proxies which allows for two way synchronization between Java and Java FX

Posted by JUG Genova on January 06, 2009 at 04:56 PM PST #

Not sure if this would worth considering at the present time, but would some form of Invoke API usage be beneficial?

This might make moving towards JSR 292: Supporting Dynamically Typed Languages on the JavaTM Platform (see in Java7 time frame a little easier.

Posted by Eric Bresie on January 08, 2009 at 01:41 AM PST #

I'm having the same problem as @David MacGibbon.

Entry.entryMethodName() looks like its hardcoded to return "javafx$run$".
Through reflection, I can tell MyScene doesn't have that. I'm not sure what to check next.

Posted by Roger Cooley on January 09, 2009 at 03:09 AM PST #

As an answer to Roger Cooley, David MacGibbon and others who might run into the same problem you should probably add something like this to your JavaFX class.
public function run() {
MyScene {}
Besides that I'm a bit disappointed that embedding a JavaFX scene in Swing isn't part of the official API. It seems like a step back from the preview release where it was something that was quite easy to achieve.

Posted by STE on January 11, 2009 at 09:41 PM PST #

JavaFX looks to be a really cool addition. I don't know much at this point but am eager to learn and use it. The lack of being able to use JavaFX from an existing swing application will prolong getting it into my toolbox of tools to use. I have a Netbeans RCP application I am currently witing that uses legacy swing code, hence Netbeans verses Ecplise, and I can really use the JavaFX JWebPane component. I'll try the hack above, but will see if I can get anywhere with it.

Posted by Steven Shea on January 11, 2009 at 11:25 PM PST #

@STE - Thank you. That put me on the right track. After the end brace of my Class defination I added:
public function run(args : String[]) {
MyScene {}

Outside of it feeling kludgy, it works. I've hosted it inside of an Eclipse RCP demo I'm working on.

Posted by Roger Cooley on January 12, 2009 at 01:41 AM PST #

+1 for better Swing -> JavaFX integration. Some cases make sense for Swing and some are perfect for JavaFX. The two should be able to work together in a predictable manner so that large Swing apps may be refactored by parts to move the right parts to JavaFX.

Posted by Sebu K on January 12, 2009 at 05:49 AM PST #

Thanks again Josh. Unfortunately as your bold text highlights this isn't a clean and reliable solution. Philosophically speaking, JavaFX adoption or (more to the point) migration from swing will take a decent hit until this is fixed/implemented.

I'd like to compliment my existing swing application with JavaFX content. Sort of like "inside out" component/widget/plugin migration. My pathway is blocked and for that matter, I guess we won't see any JavaFX driven content appear in NetBeans for quite some time either.

Thanks for the post Josh, looking forward to Sun's position on this matter.

Posted by Andrew Hughes on January 14, 2009 at 08:56 AM PST #

Looks a lot like the code I wrote using the alpha / preview builds ... and I cant stress enough how MUCH this is needed!

Another integration point is the ability to integrate with Spring - I want to have properties injected into Spring Beans that are (in fact) JavaFX classes to cleanly separate my MVC layers. Shouldnt be too bad, but there's the issue of the strange property naming when you begin introspecting into the JavaFX classes.

All this (sort of) comes together in the desire to use Spring-RCP and JavaFX. Its all close, and work like yours Josh is vital to that effort. Thanks!

Posted by Paul Hawke on January 15, 2009 at 01:03 AM PST #

+1 on a supported public API for this.

Posted by Gath on January 19, 2009 at 08:46 AM PST #

it's too complicated and absurd to develop rich client application without transparent integration with swing.

Posted by Dimitri on January 21, 2009 at 01:27 AM PST #

Another vote from me for ability to embed or make JavaFX 'play' with Swing officially + supported rather than a 'hack' PLEASE!

Posted by Alan on January 22, 2009 at 08:32 AM PST #

I very need to simple integrate JavaFX into Swing applications, do it in second release, please!!

Posted by Oleg on January 23, 2009 at 07:11 PM PST #

interesting work around. But we need to get soemthing in swing properly for this.

would compiling the script help performance at all? something like (java side)...
final ScriptEngineManager manager = new ScriptEngineManager();
final JavaFXScriptEngine fxEngine = (JavaFXScriptEngine)manager.getEngineByName("fx");
final CompiledScript compiledScript = fxEngine.compile(rawScript);
// as long as your sript has a getScene method that returns a
// JavaFX.Scene
final Object o=fxEngine.invokeMethod(compiledScript,"getScene", new Object[]{});

//set the scene variable in the helper panel as Josh example
String helperName = "com.sun.javafx.scene.JSGPanelSceneImpl";
FXClassType type = FXContext.getInstance().findClass(helperName);
FXObjectValue panelSceneImpl = type.allocate();
panelSceneImpl.initVar("scene", FXLocal.getContext().mirrorOf(o));

Posted by Steve on January 26, 2009 at 07:24 AM PST #

I found a simpler way to get the Scene object:

Class app = Class.forName(path);
Scene jscene = (Scene)app.newInstance();

This can replace the ScriptEngine method of getting a reference to the scene.

Posted by Samuel on January 30, 2009 at 02:58 AM PST #

Yes, please provide a way to use JavaFX within an existing Swing based application. For very large Swing applications such as our primary software product, there is much to gain by converting the app from Swing to JavaFX. However, its unfeasible to try to "rewrite" the entire application "as a whole". It would only be possible if we could gradually convert the application over, and that could only be done well if we could use JavaFX withing the existing Swing app easily and reliably.

Posted by Raffi Minassian on February 01, 2009 at 02:05 AM PST #

Thanks for the post. But is all this rendered completely moot by Sun's licensing? So far, there doesn't seem to be any legal way to distribute JavaFX components with one's own software(as you can with the JRE), without which there doesn't seem to be any point in integrating the two.

I've been trying to find some/any information specifically regarding the future of JMC and Swing(JDK7 preview release doesn't include JMC).

It would be a real shame if all this new functionality was made practically impossible to use commercially merely due to licensing issues.

Any clarification would be much appreciated.

Posted by Alex Shaykevich on February 12, 2009 at 12:28 PM PST #

Hi Alex. The JavaFX runtime for desktop is designed to always be downloaded on demand. Any applet or webstart based application can use it simply by including a reference to the runtime in your JNLP (or let the javafxpackager do it for you). Is there a reason you must bundle the JavaFX runtime with your app?

Regarding JMC, we plan to enable JavaFX APIs be accessible from other JVM based languages, including Java.

Posted by Josh Marinacci on February 13, 2009 at 12:49 AM PST #

I'm sure you've heard enough of this, but i agree 100%: with seamless swing application integration I'd use javafx in an instant. It seems like it could change my entire UI conception. and is exactly what I need. I was blown away by the examples. Without swing integration, and the ability to access java datastructures it will probably remain an interesting curiosity to me.

Posted by Andy Palumbo on February 13, 2009 at 04:55 AM PST #

Hi Josh,

Thanks for the reply. Surely you can understand that if one is trying to create commercial desktop Swing applications, then this on-demand integration is useless. I'm not developing Applets or Webstart apps, but distributable applications, with installers, etc...

I would love to use the JMC components for video playback in these apps and it's been the much needed multimedia support Java has needed since the death of JMF a decade ago. I say this after a long time of JMF development. The same goes for the rest of JavaFX/Swing integration. If it won't truly run in a standalone desktop environment, then it's sadly of no use to many, I dare say most, Swing developers, since Webstart is simply not a viable widespread commercial model. And again, I say this as someone who develops a Webstart app by day.

Right now, the really frustrating thing about all this is that it is simply a legal issue, since technically, others and I myself have JMC fully integrated inside Swing using the jars, dlls, and jnilibs from the JavaFX SDK distribution.

So please, can you give me an answer on the future licensing of JMC as pertaining to standalone Java Swing desktop applications?



Posted by Alex Shaykevich on February 13, 2009 at 09:58 AM PST #

Things I want to do with JavaFX in a Swing applcation

develop applications that can embed video (JavaFX).

develop a desktop (Swing) video (JavaFX) editing tool.

develop a desktop WYSIWYG (Swing) HTML editor (JWebKit / JavaFX).

develop a desktop (Swing) CAD / Game tool that displays and animates (JavaFX) a scenegraph view (JavaFX) of the work product.

Just completely off the to of my head, with 1 above having nearly infinite applications some of which this cagey entrepreneur is not tipping his hand on! LOL.

But here's the real point: "why would anyone want that?" is one of the riskiest questions imaginable, frighteningly close to "what good is THAT?".

The answer is: if you give developers the ability to use it inside Swing, you're creating synergy between two powerful systems. There are just acres of novel UIs just waiting to represent tons of deeply mined data sets in ways that just couldn't be done before. The triumvirate of HTML / Video / an animation-scenegraph framework can be used in an infinite number of ways, most of which none of us individually can think of, just like no one saw Google when DARPA Net was first rolled out.

Unless of course you're going to completely replace ALL of Swing with JavaFX. Of course that means not just Buttons n' JTrees, but all the text libraries, for example the well tempered cursor that just does the right thing with BIDI text etc. and also it's known that the each of those JavaFX replacements is as good or better with respect to loading / runtime / memory performance as their Swing cousins under the utilization profiles typically encountered in desktop applications, not RIA applications.

According to the JavaFX site, it is not Sun's intention to replace Swing on the desktop. I personally don't care, but I do care that I don't find myself in the following nightmare scenario:

Swing is understood to have been EOLed, even if Sun doesn't officially say it. Thereupon, the most powerful, mst throughly documented, easiest to use GUI toolkit ever created by any company is all but abandoned by the marketplace in what will later be seen as the RIA hysteria / bubble of the turn of the century, taking one of Sun's most compelling pieces of intellectual capital with it.

Meanwhile, JavaFX replacements are trotted out in spurts and fits, with crucial (BIDI cursor) parts AWOL because some faction of managers / engineers inside Sun don't see them as a priority given available resources and incentive schemes.

Bugs that make certain JavaFX components unusable under certain circumstances are just starting to run the clock on their five-year-before-getting-fixed life. That's not a swipe at Sun- it's true of all tool kits from every provider.

The collective energy of the UI across the world walks out the door and over to Adobe. Developers everywhere pay the Adobe tax (10k and counting) on their development tools.

OK now that's a nightmare.

You could take the idea that the network is the computer too literally. But I don't see how you can take the idea that the computer is the UI too literally. The UI is where every battle is being lost and won.

So why should you make JavaFX available to Swing? For the sake of things that have never been able to be written.

Posted by swv on February 14, 2009 at 09:13 PM PST #

C´mon guys, you can not be serious to create such a great thing like fx and then simply cut it down to applet 2.0. Please add a permanent FX component to swing and open the full api to swing apps. This might give you an edge over MS WPF, which Swing ourdays simply can not compete with.

Posted by Big Foot on February 14, 2009 at 09:44 PM PST #

Hi Josh,

could it be that this sample is now broken with the JavaFX SDK 1.1? Using JavaFX SDK 1.1 I am getting:

java.lang.NoSuchMethodException: javafxswing2fxfx.MyScene.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)
at java.lang.Class.getMethod(
at org.jdesktop.swingx.JXScene.setScript(
at javafxswing2fxjava.Main.main(


Posted by Peter on February 23, 2009 at 02:00 AM PST #

Hi Josh,

one correction: I was using the last updates for JavaFX for NB6.5, but I think they implement JavaFX SDK 1.1.


Posted by Peter on February 23, 2009 at 02:07 AM PST #

Everyone already said some pretty good arguments about why swing/JX integration is a MUST, but remember that JavaFX was Sun's move to get into the RIA market that has been growing at an amazing speed. So pretty much it's to get a peace of the RIA cake that for the moment it's in dispute between adobe and microsoft.
To make a comparison with Adobe's product, and why the swing/JX is a MUST, adobe has flash to build the components, flex to integrate them into an application, and adobe RIA for deployment.
The problem with the current JavaFX strategy would be like releasing flash for both components and application building, and RIA for deployment, and if we want to learn something from adobe's experience, we know that using flash for everything it's just too hard and time consuming, that's why flex was born.
So putting it in JavaFX experience, JavaFX is flash, applets 2.0 is adobe RIA, we are missing the flex part here, or things will get too hard. Sun doesn't need to build a flex solution, we already have it, the name is Swing.
So in conclusion, yes, I also vote for swing/JX support and full integration

Posted by Raul Guerrero on February 26, 2009 at 01:28 AM PST #

For all who get an NoSuchMethodException: MyScene.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)

Posted by Peter on February 26, 2009 at 07:07 PM PST #

If there is interest? If there is ... HELLO?!

Hello Sun!?! Do you remember that you have a UI toolkit? It's called Swing. And there are 1-2 programmers out there that already have some little Swing applications ready and deployed.

The question IF there is interest is .. OBSCURE for me. I'd love to use JFX but - hell, I won't rewrite my complete GUI JUST to use JFX!

Please provide a real way to embed JFX in SWING ans you'll see there are plenty more users out thereusing it!!

Posted by INTEREST! on March 24, 2009 at 06:48 PM PDT #

+1 Yes please! It's mind-boggling we even have to vote for this...

Posted by Peter De Bock on March 31, 2009 at 11:57 PM PDT #

We are using the JMC parts inside a Swing up very successfully.

Can the world get a clear statement on the licensing of JMC?

It is clear that I can't ship JavaFX with my app, sadly, but what about JMC.
We tied it very nicely into JMF and it dies way cool playback on Mac OS X!

If the licensing is unclear, we will have to abandon JMC and look for alternatives or start implementing code-level stuff by ourselves, which we clearly don't watn to do...


Posted by Werner Bauer on April 02, 2009 at 07:20 PM PDT #

Yes please. :)

I agree with the others responding to the "question". Of course swing users will wan't to use the new cool effects and the ease of writing good looking effects.

I also think the question, from my point of view, should be: "Why would you not create a from-swing-API?" What kind of difficulties would arise from making this happen? How much work would it take on your part. I think alot of people feel that your question is phrased in a way that is the direct opposite of how they are thinking. So, to get your side of the view, the reason behind this being a question at all, it would be nice if you could explain the difficulties you see.

Finally, I think you have a solid and numerous set of people out there that could help promote JavaFx by creating widgets, sharing experiences and so on. You are missing an opportunity to let the "old world" embrace JavaFx. The easier it is to access this new technology, the wider the adoption. I can't use JavaFx in my work now, since my work is based on Swing. Hence, I fail to see wy I should learn JavaFx. Why wouldn't I learn any of the other technologies instead, if I'm starting a new project? The answer to that would be crystal clear if I could access JavaFx from Swing. The answer would be: "Because you can use it in existing projects too, learn incrementally, widget by widget and you can show off to your boss." Choosing Adobe or MS will not let me do this. But of course, nor will JavaFx at the moment.

I would slowly adopt JavaFx because it would be readily available to me in my daily work. When starting a new project I would naturally choose JavaFx. If you instead choose to not let me access JavaFx from Swing, I will not learn it as part of my work. In that case, I could just as easily learn Air or Silverlight.

Posted by Per-Erik Svensson on April 30, 2009 at 02:18 AM PDT #

I support embedded javafx to Swing or SWT. My client asked me do some research on javafx to see whether can use javafx in ecliplse RCP project. I told him no, because it is not supported by official.

Posted by YongHu on May 11, 2009 at 06:56 PM PDT #

Yes for media (video streaming for ex) support within Swing using JavaFX.


Posted by Thierry Janaudy on May 13, 2009 at 01:20 AM PDT #

"Write once, run anywhere"

Please allow the Java Community to easily embed JFX in Swing and all will win.

Thank you.

Posted by HFernandes on May 14, 2009 at 06:02 AM PDT #

Yes, please add a permanent supported API for JFX in swing.

Posted by sb on May 29, 2009 at 01:45 AM PDT #


JavaFX should be inside Swing, not the other way around. There are huge Swing extensions out there (JIDE is an example). The binding mechanism can only be used in isolated component, and it's a suicide to build a big app this way. Everything is executed in EventDispatchThread. Swing developers have solutions for it. 5 months have passed since JavaFX 1.0, and very few advancements since then. Perhaps it turned out that writing software takes time, so how much time would it take to replace Swing?

Posted by Alex Ryzhov on June 01, 2009 at 10:08 AM PDT #

Does anyone actually have a simple working example of this with JavaFX 1.1 ? With this example I get:
java.lang.NoSuchMethodException: jfxtest.SwingHook.javafx$run$(com.sun.javafx.runtime.sequence.Sequence)

adding a run method just gives me another exception:
Exception in thread "main" java.lang.NoSuchMethodError: javafx.reflect.FXLocal$Context.mirrorOf(Ljava/lang/Object;)Ljavafx/reflect/FXObjectValue;
at org.jdesktop.swingx.JXScene.setScript(

Trying the workaround posted by Peter gets me no where either since the Java code will not compile. It can not find a jfx class in a jar that is included in the build path, and NetBeans does not allow mixing of JFX / Java in the same project.

If someone can post or send me a complete file that works, I would greatly appreciate it. Id rather not have to wrap all my Swing components in JFX wrappers...

Posted by Michael Hoglan on June 03, 2009 at 03:28 PM PDT #

Scratch that...

If you are having issues getting either example working, check and make sure your NetBeans is not using some internal JFX SDK that you are not aware of. You can check the library settings and see where the JARs it is including live.

This example works on JFX 1.1, my JFX file looked like:


package shapes;

import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.Scene;

public class MyScene extends Scene {
init {
content = [
Circle {
centerX : 140
centerY : 80
radius : 20
fill : Color.LIGHTGREY

function run(args : String[]) {
var myScene = MyScene { }

In my Java program I had:


import java.awt.*;
import javax.swing.*;

import org.jdesktop.swingx.JXScene;

import shapes.MyScene;

public class SwingJFX {
public static void main(String[] args) {
JFrame f = new JFrame("This is a test");
f.setSize(400, 150);
Container content = f.getContentPane();

JXScene scene = new JXScene();



Eclipse seemed to work better when importing a JFX jar as an external jar and referencing it. I could not get NetBeans to recognize the JFX jar as an available package.

Good luck :)

Posted by Michael Hoglan on June 04, 2009 at 06:47 AM PDT #

The hack doesn't work with JavaFX SDK 1.2:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.javafx.scene.JSGPanelSceneImpl
at javafx.reflect.FXLocal$Context.findClass(
at javafx.reflect.FXLocal$Context.findClass(
at javafx.reflect.FXLocal$Context.findClass(
at test.JXScene.setScene(
at test.TestFX$

I checked, there is no class com.sun.javafx.scene.JSGPanelSceneImpl anymore. Does anybody know how to modify it for 1.2?

Posted by Alex Ryzhov on June 04, 2009 at 12:27 PM PDT #

[Trackback] &#8220;Insider&#8217;s Guide to Mixing Swing and JavaFX&#8221; lautet der Titel vom Amy Fowlers Blogeintrag. Ich habe mich schon gefreut, dass es mit JavaFX 1.2 endlich möglich ist, JavaFX in Swing einzubetten um so die neuen features in bestehenden Sw...

Posted by Frickelblog on June 11, 2009 at 04:12 AM PDT #

[Trackback] &#8220;Insider&#8217;s Guide to Mixing Swing and JavaFX&#8221; lautet der Titel vom Amy Fowlers Blogeintrag. Ich habe mich schon gefreut, dass es mit JavaFX 1.2 endlich möglich ist, JavaFX in Swing einzubetten um so die neuen features in bestehenden Sw...

Posted by Frickelblog on June 11, 2009 at 04:13 AM PDT #

I am starter in javafx.How we can use frame in javfx if iam using javafx netbean 6.5.1. i tried but its not working .

Posted by banita on June 14, 2009 at 04:11 PM PDT #

I did not tried but in JavaFX1.2 there is a class com.sun.scenario.scenegraphe.JSGPanel which extends javax.swing.JComponent

Posted by Sebastien Stormacq on June 22, 2009 at 03:28 PM PDT #

We have been trying to use this with 1.2 and We have not found how to do it. We want to add JavaFx into our Swing development but apparently, you need to translate all the development to JavaFx to be able to use it. If there is no API that allows inserting JavaFX on Swing, unfortunately we will not be in the position to continue trying to include JavaFx into our application, but we are really keen on having a way to do so.

Posted by Francesc Mart&iacute;nez on July 01, 2009 at 12:48 AM PDT #

yes plz release an API to integrate javafx in swing

Posted by Ray on July 07, 2009 at 12:53 AM PDT #


in my opinion, using javafx out of swing apps is a very, very useful feature to add! Think of the possibilities! Imagine for example the following scenario:
* use Matisse (the netbeans gui builder) to set up the general UI of an app
* use JavaFX for all the more complicated and dynamic UI stuff (using effects, painting charts, detailed pop ups, whatever)
I'm quite new to JavaFX and I don't really know if the latter is realistic, but it would be very useful and potentially could speed up application development significantly!

Go ahead guys!


Posted by paschelino on July 08, 2009 at 06:28 PM PDT #

Just to add my 2 cents concerning "Interest?": YES-YES-YES-YES-...

JavaFX could be *the* thing for up-to-date commercial Swing Apps!!!!

javaFX could be just *the* thing Swing desktop apps need to cope with the needs of contemporary UI design. Swing is a very broad and quite mature base for desktop applications, but creating good-looking UIs with animations etc. in Swing can result in a major PITA. Thus I understand very well that there are just a few how venture to create up-to-date UIs for desktop apps in Java - and that means: who venture to choose Java as a platform for desktop apps. At least this is the case for many SMBs as far as I see.
Having JavaFX with all the eye candy and the animation would speed up development enormously - not only by a factor of 10!!!.
This would have the power to change Swing into the "killer app" among desktop software platforms!
What does it take to convince Sun Management that JavaFX is vital for the success of Swing (that means: Java Desktop Apps!) - and not a replacement for Swing, at least not as long as it does not provide the full set of UI components and features that Swing does.

Posted by rob.rohm on July 30, 2009 at 12:43 AM PDT #


If you are looking for the easy way to include JavaFX components inside Swing (Francesc Martíne) your jurnay has ended :) just read my post on my blog:

Posted by Michał Margiel on July 30, 2009 at 12:55 AM PDT #

Hi Michal,
that's great news!
Very interesting work, you did!
Would you mind to share some of your experiences with that method?
It would be very interesting ...
... which JavaFX-components you were working with (in Swing)
... what difficulties or drawbacks you faced

@the JavaFX Team:
what do you say to this aproach?

Thank you very much!
Best Regards,

Posted by rob.rohm on July 30, 2009 at 06:15 AM PDT #

Hi Rob.
Hare are [A]nswers for all your [Q]uestions.

[Q]... which JavaFX-components you were working with (in Swing)

[A]Well... many of them :)
As I mentioned on my blog we have created pretty complex table component (build on top of the JFXtrass Grid Layout), and even more complex Graph component (with selection some period of time, zooming in/out). Both of them utilize many JavaFX objects - Rectangles, Lines, Animations, TextBoxes, Layouts and so on...

[Q] ... what difficulties or drawbacks you faced

[A] I don't know why but JavaFX layouts[1] are not working properly in Swing - they are not layouting objects automaticly, we had to create and invoke manually special method to force layout (which is called forceLayout() ;) ).
But the main problem for us is that creating of JavaFX components (even simple Rectangle) takes really long time - what is killer for our Table, and we will have to rewrite it to normal Swing component.

I don't remember any other issue, and what is interesting we have notices that JavaFX components inside Swing works a lot faster!

[1] to be honest I don't remember if this problem is only in JFXtras layouts or also inside original JavaFX layouts

Posted by Michał Margiel on July 31, 2009 at 02:45 AM PDT #

Hi Rob.
Hare are [A]nswers for all your [Q]uestions.

[Q]... which JavaFX-components you were working with (in Swing)

[A]Well... many of them :)
As I mentioned on my blog we have created pretty complex table component (build on top of the JFXtrass Grid Layout), and even more complex Graph component (with selection some period of time, zooming in/out). Both of them utilize many JavaFX objects - Rectangles, Lines, Animations, TextBoxes, Layouts and so on...

[Q] ... what difficulties or drawbacks you faced

[A] I don't know why but JavaFX layouts[1] are not working properly in Swing - they are not layouting objects automaticly, we had to create and invoke manually special method to force layout (which is called forceLayout() ;) ).
But the main problem for us is that creating of JavaFX components (even simple Rectangle) takes really long time - what is killer for our Table, and we will have to rewrite it to normal Swing component.

I don't remember any other issue, and what is interesting we have notices that JavaFX components inside Swing works a lot faster!

[1] to be honest I don't remember if this problem is only in JFXtras layouts or also inside original JavaFX layouts

Posted by Michał Margiel on July 31, 2009 at 02:50 AM PDT #

I have tried something very similar to what was shown by MichaƂ Margiel, even though it did work, I detected some synchronization problems. My "component" was simply a rectangle, but not everytime I started the app the Rect would be shown. Most of the time the app would freeze during what I guess is construction of the GUI. I think there might be some issues concerning threads in Swing and JavaFX. Did any of you have similar reliability problems? And the same question again, has anyone had success with an elaborate component. In my case I am implementing a Gantt Chart, so many rectangles, zooming and scrolling.

Posted by Esteban on August 31, 2009 at 12:15 AM PDT #

check this class JFXPanel in

whit this can do:

+ setScript(Object aScript, String scriptName)

and it extend from JPanel!

Posted by emiliooo84 on September 01, 2009 at 12:08 AM PDT #

Hi Josh,

I tried to embed fx scene in swing. it worked fine. Now in Fx Scene I was generating nodes dynamically based on my datastructure. I used HBox and VBox for horizontal and vertical layouts.

But when this scene was added in JPanel there was no layout.

So I wish to know, is HBox and VBox layout not supported when we use scene in swing using java reflection.


Posted by Raghav on September 02, 2009 at 12:25 AM PDT #

Hi Josh,

I've created a 1.2 and 1.21 javafx compatible JXScene. It's available on the JFXtras repository.

I'm having troubles playing video on Vista and Windows 7 using this JXScene approach. Can you tell me what might be the problem? Or where can I get help for this?


Posted by Pedro Duque Vieira on September 22, 2009 at 01:49 AM PDT #

Are there any news about official JavaFx in Java integration? I believe there was enough interest in the feature for Sun to add it to Roadmap. Personaly, I think JavaFX wont survive without this integration.

Posted by nau on October 04, 2009 at 11:08 PM PDT #

Yes I hope JavaFX can easily and properly be embedded in to Swing. I personally see reasons to do this and no real personal reason to use JavaFX any other way.

Posted by Edward on October 20, 2009 at 10:49 AM PDT #

I don't know why but JavaFX layouts[1] are not working properly in Swing - they are not layouting objects automaticly, we had to create and invoke manually special method to force layout (which is called forceLayout() ;) ).
But the main problem for us is that creating of JavaFX components (even simple Rectangle) takes really long time - what is killer for our Table, and we will have to rewrite it to normal Swing component.

Posted by sohbet on November 11, 2009 at 12:50 AM PST #

Is there any progress on this so far?
For me this is a key feature for JavaFX!

Posted by burni on November 15, 2009 at 10:11 PM PST #

Is JavaFX going to die?

Posted by burni on November 23, 2009 at 04:04 PM PST #

GOOD post ,It's useful,I like it.HOHOHOH

Posted by abercrobmie and fitch on November 27, 2009 at 01:57 PM PST #

There seems to be a severe oversight in the swing integration. The pattern should NOT be to call the create|createJComponent method from within the CustomNode|SwingComponent ctor. It circumvents the possibility for JavaFX classes to initialize the component as a def instance member.

That is, instead of this pattern (as it is currently required):
class MyNode extends CustomNode {
public override function create() { return ... }
or, for Swing
class MyNode extends SwingComponent {
public override function createJComponent() { return ... }

the pattern should be
class MyNode extends CustomNode {
init { create(...); }
or, for Swing
class MyNode extends SwingComponent {
init { createJComponent(...); }

I've noticed that it'd be very handy to be able to do the following when implementing Swing wrappers:

class MyNode extends SwingComponent {
def component = JLabel{}
init { createJComponent(component); }

But that's not possible since MyNode.init doesn't execute until AFTER SwingComponent.createJComponent is called so, currently, the component variable will always be null.

The reason the above is a useful pattern is to be able to "overload" Swing g/setters with JavaFX instance variables as follows:

class MyNode extends SwingComponent {
def component = JLabel{}
var text on replace { component.setText(text) }
init { createJComponent(component); }

Of course, that's only useful when the Swing content is completely managed from JavaFX (since it's one-way, JavaFX->Swing), which reveals, what i consider, a MASSIVE OVERSIGHT with JavaFX. That is, is doesn't understand Beans. o_O Why in the world would you create a new language based on Java and not implement the capability to do this:

var j = JLabel {text:"This makes perfect sense"}

Which, in java, is simply:

JLabel j = new JLabel();
j.setText("This makes perfect sense");

If the above pattern were possible, immediately, all Swing components are native JavaFX components and incorporated into any Scene with a simple SwingComponent.wrap(). That pattern is really useful as it, one-way, because you can then do:

var j = JLabel {text: bind ... }

But it'd also be very nice to be able to do this:

var jlabel_text = bind myNode.component.text

Though that's probably no small chore (I know, since i've written many examples to try and find a reasonable pattern for generalizing it).

Anyway, i've digressed. My primary point is the Node/Swing initialization issues. I believe it needs to be resolved to facilitate the above pattern i've outlined. At least then, sending messages to swing is real, native JavaFX instead of feeling like mixing in Java.

Posted by nicerobot on December 05, 2009 at 08:15 AM PST #

First, let me say, i really do like JavaFX A LOT even with the bean-issue i stated above. Though i do think that issue is a seriously, massive oversight.

That said, i've also revised my gripe a little having discovered a somewhat nice declarative way to implement Swing components in JavaFX:

content: [ SwingComponent.wrap({
var label = JLabel{};
jlabel.setText("My Label");
jlabel; }]

While that's not horrible and circumvents the necessity of creating a subclass for every JComponent being implemented, it's still FAR too unwieldy, especially once it gets a few levels deep.

This is what is should be able to do (as per my prior post):

content: [ SwingComponent.wrap(JLabel{text:"My Label"}) ]

but this would be acceptable:

content: [ SwingComponent.wrap(JLabel{postinit{setText("My Label")}}) ]

The idea being that JavaFX should retain declarative-feeling capabilities even for java-native classes.

Posted by nicerobot on December 09, 2009 at 09:50 AM PST #

i am trying to load FXGraph(Area chart) into swing application with the above code but i am not gertting it.

Posted by Pavan on April 27, 2010 at 04:16 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Insider News from the Java Team at Oracle!



« June 2016