JavaFX and OSGi, part 2

In my previous blog entry, I described a method to execute an OSGi framework in a JavaFX runtime. In this entry, I show how you can write OSGi Bundles containing JavaFX code, and how they can be installed into the OSGi platform running in the JavaFX runtime.

The idea behind this code might be a bit strange and dumb (why would one want to write an OSGi bundle containing JavaFX and deploy it in an OSGi container on top of JavaFX?) but I have two reasons why this is really a practical use case.
In the hopefully not too distant future, we might have more devices that come with JavaFX preinstalled. My HTC Diamond phone, for example, has a JavaFX platform. If I want to install an OSGi framework onto this phone, it has to run on top of the JavaFX platform.
Also, more developers are getting used with OSGi. The JavaFX platform provides interesting front-end capabilities for OSGi based services. With JavaFX, front-end development is in general much easier than with any (A)WT framework I know.

The OsgiFX framework

Running an OSGi framework on top of JavaFX is described in this blogentry, and the code can be found at Note that I made some major changes to the code since I wrote the previous blog entry --- I hope to find some time to discuss those in a next entry.

When starting the OSGiFX application, an OSGi Framework is created (I use the Knopflerfish framework), and an org.osgifx.FxServiceListener instance is registered, listening for implementations of org.osgifx.view.JavaFxView
This interface acts as a bridge between the registered OSGi services and the OSGiFX application that will manage and display the services. JavaFxView is a plain Java Interface, with two methods:

  • getTitle() provides the title of the registered application
  • getRootNode() provides a javafx node that is the container for all ui-stuff in the application.
The OSGiFX application maintains a list of registered JavaFxView implementations, and will display an application when the user clicks on a name --- yes, it is sort of a Service Browser on top of OSGi.


I wrote a silly OSGi bundle that registers a JavaFXView implementation containing a JavaFX custom node with a nice circle.
You can find the code and manifest file at Again, I hope to find more time later to document this better, but I think the code in the BundleActivator is self-explaining:
  override function start (bc: BundleContext) {
    println ("[JVDBG] Starting bundledemoactivator");
    var view: MyView = MyView{};
    var impl: JavaFxViewImpl = new JavaFxViewImpl (view);
    var props :Dictionary= new Properties();
The MyView class contains a CustomNode with the circle, and the JavaFxViewImpl class will return this CustomNode when its getRootNode() method is called.

When bundling this code into an OSGi bundle, we have to make sure we import the needed packages. The JavaFX compiler creates some dependencies on implementation packages, these should be exported by the OSGiFX platform and imported by the bundles that rely on them.

This was a very quick intro to what I have been doing with OSGi and JavaFX, feel free to mail me if you have questions or remarks (johan at lodgon dot com).

written on 24 Nov 2009 20:32.

no comments

Create comment