Oct 20 2008

Rhino + JavaScript + Swing, Look Ma No Java

Published by at 7:50 am under Technology   

A while back I was discussing the future of programming languages with a colleague, and we agreed that for all its foibles, JavaScript will continue to enjoy very wide and increasing use in the coming years. I wrote last year about Steve Yegge’s hints that JavaScript is the “next big languages”, see that post for the reasoning.

Based on all that, I set about writing a small test app to see what it’s like to program a Swing app with JS.  After a day or so of work (spread over a few months), I offer my Rhino Swing Test App:

Run RSTA now via Java Web Start

Get the RSTA code (git, on GitHub)

It implements the same “flying boxes” animation demo that I presented a few years ago at the St. Louis JUG, but aside from a generic launcher class, the GUI is implemented entirely in JavaScript. To clarify, this is not web browser JavaScript; it is running in Rhino, in the JVM, using Swing classes.

The documentation for interaction between Java and JS is limited, but sufficient. For simplicity, I used Rhino as an interpreter, I did not compile to java bytecode. Nonetheless, the animation runs about as smoothly in JS as it does in Java, because the heavy lifting is done by the JDK classes.
I used Eclipse (with JavaScript support) to write this code, but of course JS makes much less code completion possible than Java, and I missed that. Typically I mildly prefer IDEs, but am also productive with a text editor. For working with a large API like Swing though, IDE support helps greatly.

Still, I recommend a look at this approach if you are fond of dynamic languages but need to build on the Java platform, and I intend to investigate server-side JS development also.

If you found this post useful, please link to it from your web site, mention it online, or mention it to a colleague.

4 responses so far

4 Responses to “Rhino + JavaScript + Swing, Look Ma No Java”

  1. Ivan says:

    I’ve been working on a Javascript-based server-side framework for a while now. I’ve found Aptana to have the best out of the box Javascript-editing experience. Once you install it you should enable the validators which complain a lot, but you can filter accordingly. The best validator I’ve found has been the JSLint one.

    Likewise the newest Eclipse EE version includes a Javascript auto-completer of some sort, but I wasn’t able to get it to auto-complete out of the box. You might want to look into it.

  2. rick says:

    I assume this is compatible with all browsers because it is byte code. Do you gain anything by using javascript? It sounds like scope problems will prevent you from working with Javascript on the client.

  3. Kyle Cordes says:

    Rick – This sample show client-side JavaScript, inside a Java client application.

    It works with any browser that support Java Web Start; it has nothing whatsoever to do with JavaScript in the browser itself.

  4. Barclay says:

    Does anyone out there know where I can find a good browser written in java that can handle latest versions of javascript and ajax and have a good HTML rendering ability? We are looking into building a crawler that can extract contents from HTML pages that contain javascript and dynamic page generation. Thanks in advance. Barclay