Using JavaFX in Jython Applications

Submitted by: Josh Juneau

http://jj-blogger.blogspot.com

Have you heard about JavaFX script yet? Of course you have, it is one of the hottest topics in the Java world today. Although in it's early stages, JavaFX script proves to be an easily learned and powerful scripting language addition to the Java community. It gives one the ability to easily create Java animations and Swing applications without coding any Java at all. However, sometimes it is still useful to combine Java and JavaFX to create more robust applications. For example, if one wishes to build an RSS reader using JavaFX then it is still handy to use the Java language for performing RSS parsing. This is because JavaFX script seems to be more targeted at creating user interfaces than for core Java programming.

It is because of the added benefit of using Java and JavaFX script together that Jython and JavaFX are a great combination. Most people would like to argue that Jython is also a good substitution to core Java programming. We have the ability to create Jython applications and invoke JavaFX user interfaces from within them...and it couldn't be simpler!

This short article does not contain any complex Jython code...it is very simplistic, but it is a good introduction to incorporating JavaFX into your Jython applications.

Setting Up the Environment

First of all, we need to set up our environment. The easiest way to get started with JavaFX and Jython is to begin with the command line. You need to ensure that the javafxrt.jar and swing-layout.jar Java archives are included in your classpath before running this small demo. You can obtain these jars from the OpenJFX project pages.

Once you've obtained the JAR files, then create a directory on your hard drive and place the jar files into that directory. Let's call the directory "jythonfx" for the purposes of this article. You then need to set the classpath using the following syntax

Windows 2000/XP/Vista

Open command prompt, change to the jythonfx directory and type the following:

set CLASSPATH=path_to_jythonfx_directory\javafxrt.jar;path_to_jythonfx_directory\swing-layout.jar;%CLASSPATH%

Mac OSX 10.4 or 10.5

Open terminal, change to the jythonfx directory and type the following:

export CLASSPATH=/path_to_jythonfx_directory/javafxrt.jar

export CLASSPATH=/path_to_jythonfx_directory/swing-layout.jar

Keep this command line or terminal open because this is where we will run the application.

Coding the Application

This simple demonstration uses only two files, one of them is a Jython script, and the other is a JavaFX script. The idea is to invoke the Jython script, which in turn invokes the JavaFX script. For the purposes of this demo, we will not do anything constructive with the Jython. However, we could create an entire Jython application and use JavaFX as the front end UI using a similar approach.

The Jython Code:

fxLauncher.py

# fxLauncher.py
#
# Jython script to programatically invoke a JavaFX script contained in the same directory
#
from java.lang import String
from net.java.javafx import FXShell

# Invoke the JythonFx script using the FXShell
def fxLauncher():
   name = ["JythonFx.fx"]
   try:
      FXShell.main(name)
   except Exception, e:
      print e

if __name__== "__main__":
    fxLauncher()

This simple JavaFX script creates a Swing frame and prints a simple line of text.

JythonFx.fx

import javafx.ui.*;
import javafx.ui.canvas.*;
        
Frame {
    title: "Hello World!"
    width: 300
    height: 100
    content: Label {
        text: "Hello World with Jython"
    }
    visible: true
    
}

As you can see from the code above, creating a simple Swing UI is easy and the code is brief. In order to invoke the application, you must ensure that these two files reside in your jythonfx directory, and then invoke the script using the following line in your command line or terminal.

jython fxLauncher.py

Conclusion

As you can see, it is quite simple to invoke a JavaFX program from within Jython. Is it useful? That depends on what you would like to accomplish with your application. Personally, I think JavaFX is going to become even more of a hot topic once Sun releases it's JRE. If you are using Jython to create servlets, then I think it will be possible to invoke JavaFX applets using similar techniques as the one described above. The technology is there, now it is time to have some fun and use it.

Resources

JavaFX: First Steps

Programatically Invoking JavaFX Script

JythonMonthly/Articles/December2007/2 (last edited 2008-11-15 09:15:58 by localhost)