Seamless Web Browser Integration
The complete title for this JavaOne 2007 BOF was Ingredients for a Killer Application – Adding Mojo to Your Swing and Ajax Applications with Seamless Web Browser Integration. Wow, what a mouthful. It might not be immediately clear from the title but the focus of this BOF was the use of the JDesktop Integration Components (JDIC) Web Browser embedded in a Swing application. As we all know Swing does not a complete Java-based web browser component, although there is a good XHTML Renderer. With the JDIC you can embed a native browser (IE, Firefox, or Safari) in your Swing application. Unlike the XHTML Renderer, the native browser allows you to run JavaScript code, apply AJAX/Web.2.0 effects, manipulate the DOM, and more.
Integrating JDIC into your project is as simple as adding the jar. A key demonstration of this BOF was to show how to have bi-directional native browser communication with your Swing application. To use the JDIC native browser, just create a WebBrowser object and invoke the executeScript method passing some JavaScript code in a string to communicate from the JVM to the browser.
To work your way from the broswer to the Java process you need to listen to the statusTextChange method on the WebBrowserListener. The statusTextChange method would be called when you set a string value to the browser window, as in the following JavaScript code.
[source:javascript]
window.status = “message passed to the JVM”;
window.status = “”;
[/source]
The speaker did warn the audience that using the status text to listen for processing instructions from the browser was a total and complete hack. The speaker recommended that as soon set the status to communicate with the JVM that you clear the command/message. The speaker talked about having your own mini-protocol to pass data and instructions to the JVM, I would recommend you use a standard like JSON.
Someone in the audience asked about having access to the x, y location of DOM elements in the browsers to combine with Java 2D overlays and effects, but the speaker was not sure how to go about implementing such a task.
Using the JDIC native web browser in your Swing application you can take advantage of Web 2.0 services such as Google Maps and Flickr in a Swing/Web 2.0 mashup. I have been looking into the JDIC web browser so that I can create a offline standalone web-based application by embedding a browser, Jetty, HSQLDB, and JRuby on Rails. Embedding all these components is easy enough to integrate, although you might have to write some glue code to make into a fully stacked framework.
Technorati Tags: javaone, javaone2007, jdic, browser, xhtmlrenderer, rails, jruby, jetty, hsqldb, web.20