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.

window.status = “message passed to the JVM”;
window.status = “”;

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: , , , , , , , , ,

6 Responses to “Seamless Web Browser Integration”

  • Ian Heffernan Says:

    I am developing a standalone webapp with a JDIC browser embedded. I’m using the execute script tag on the browser to read the meta-tags. Would you know how I go about passing these values back to the Swing application?

    Thanks for your help


  • Ravi Says:

    I am trying to use JDIC in my swing application to embed firefox in Linux platform. This does not work, after lots of googling i think jdic does not support firefox (IE, Mozilla only). It works fine for IE in windows. Can you please help me on this?


  • TechKnow Says:

    @Ravi – I don’t use Linux and I haven’t tied JDIC with Firefox. If you figure it out and write about it let me know…

  • Ravi Says:

    Sure i will keep you updated.

    thanks anyway

  • Digeratist Says:

    I’m using the JDIC WebBrowser object with IE on XP, and I’m finding it very flaky. It crashes. Always. I’ve tried stripping out as much use as possible of the listener callbacks–including removing it completely–and still, no luck. Sometimes it takes several hours, but always, inevitably, it *will* freeze/crash, and it’s driving me nuts. I’m starting to think there’s a fundamental flaw with trying to use IE for this. And I can’t figure out how to get Mozilla to work, either–I’m suspecting it may only work under Linux…


  • Hans Says:

    From what I’ve gathered on the jdic forum and otherwhere, JDIC is not stable. I’ve found the DJ Project but have not tested it. Looks promising though.

Leave a Reply