As part of our, seemingly unending, refactoring of Robonobo, we packaged the whole app into an applet. However, writing a UI in swing felt very un-webby, and would fail to integreate, visually, with the style of the site. We would also like our users to have the ability to be able to customize or rebuild their own UIs using simple standards. Luckily, however, applets have some very cool features that I only just learnt about, inlcuding LiveConnect (from whence XPConnect comes from). This provides a bridge between java and javascript, allowing me to call java functions from javascript and return real java objects to javascript.
Using this, we created an applet with no UI, that sits in a 0×0 pixel container, and a javascript support library which communicates and wraps the functionality provided by robonobo. All the UI can now be rendered using html, css and javascript, in a very ajaxian fashion.
There are a couple of gotchas though:
- You cannot return primitive types such as int, float, etc. Instead you must use the object equivilents Integer, Float, etc.
- Calling from javascript comes from an unprivileged thread, so despite your applet’s policy, it may well be denied permissions. You can simply wrap such requests on the java-side with an AccessController.doPrivileged()
- Large applets can lock up the browser because the JVM and the jar are being loaded concurrently. This can be aleviated by splitting your applet into a smaller jar containing just the applet and interfaces, and the main jar containing the implementation. This way, the jvm and applet initialize quickly, and then do not block the browser when the second jar is downloaded.
