Monday, July 21, 2008

Embedding Mozilla in Java SWING Applications:JDIC and Mozswing

Sorry for the delay, I have been doing some overtime at work and have been pretty lethargic outside of it. Anyway, on with the article!

If you need to embed a web browser into a Java Swing application on Windows, you have many options, of which I have found the most useful is JDICplus, or JDIC itself. Both of these approaches are very well documented with excellent sample code.

However, doing this in a Linux environment is a little more complicated. I succeeded using no commercial software. There may be much better methods, but this is what worked for me.
I was initially able to get the package up and running using Mozswing. There is currently next to no documentation, but looking at the provided sample code helps a great deal. It uses the Xulrunner application from Mozilla to embed the browser. I did experience some instability (probably due to their Java/GTK libraries) under certain conditions, but they were easy to spot. The issues seem to be present if the Java application uses a large number of JNI calls. If you experience instability, I recommend using JDIC as discussed below.

Pros:
  • Embeds a browser as a JPanel, fully compatible with the rest of the Swing application
  • Tri-Licensed (GPL, LPGL, Mozilla), so it can be used in all the same situations as Mozilla.
  • Uses an up-to-date version of Xulrunner
Cons:
  • Early in development, currently at Beta 1 release
  • Some instability issues with Java applications that make heavy use of JNI
  • Many JAR and SO libraries
Because of the stability issues I tried out JDIC. Although at the time of this writing, the front page says it has not been updated since 2006, looking at the download page shows some active development. JDIC proved to be very stable, no matter the situation, and had few JAR and SO library files to carry around. It does embed the browser as an AWT component, so in some situation that may be an issue. However, I used it in an application with a very rich Swing GUI, and had no problems with the browser window drawing/resizing as necessary. I was also only able to use JDIC with Mozilla 1.7.13, which is kind of old. OK, fine. Really old. JDIC does not work with Firefox, and for some reason on Linux, embedding Seamonkey causes the scrollbars to disappear.

Pros:
  • Very stable
  • Licensed as LGPL, not GPL
  • Very little JAR and SO baggage to distribute
Cons:
  • Only works correctly with old version of Mozilla.
  • Embeds as an AWT component, which could cause problems in some Swing applications.
During this process I was able to find very little information on this subject. If you are faced with this problem and are unable to use a commercial library to solve the problem, I hope this information can help you piece together a solution.