Christopher has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

New Native Swing goodies: Web Browser with XPCOM, Syntax Highlighter, HTML editors

  • submit to reddit

The DJ Native Swing project is quite a success: offering its component suite (JWebBrowser, JFlashPlayer, JHTMLEditor, JVLCPlayer, ...) as an open source project allows to gather good ideas and realistic use cases that help shape its evolution. The last official release is no exception.

JWebBrowser XPCOM

One of the oldest requests was Mozilla XPCOM support, which was not present due to technical challenges. Nevertheless, there was so many requests that I had to think more about it, which eventually yielded a solution.

Accessing the Mozilla interfaces when using the XULRunner runtime in a JWebBrowser is now as easy as this:

JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.useXULRunnerRuntime());
nsIWebBrowser iWebBrowser = MozillaXPCOM.getWebBrowser(webBrowser);
nsIDOMWindow window = iWebBrowser.getContentDOMWindow();
nsIDOMDocument document = window.getDocument();
nsIDOMNSHTMLDocument nsDocument = (nsIDOMNSHTMLDocument)document.queryInterface(nsIDOMNSHTMLDocument.NS_IDOMNSHTMLDOCUMENT_IID);

JHTMLEditor on TinyMCE

There are many Javascript HTML editors available: some are small, some have loads of features, some handle popups in certain ways, etc. I originally decided to go for FCKeditor when creating the JHTMLEditor but some users were asking for other implementations.

One of these users (Jörn Heid) provided a rewrite based on TinyMCE, so with a little bit of cleanup the JHTMLEditor can now use FCKeditor or TinyMCE. Simply adding the right zip file to the classpath and the first one that is found is used. If you happen to have both in your classpath, you can simply specify the one you want:

JHTMLEditor htmlEditor = new JHTMLEditor(JHTMLEditor.setEditorImplementation(JHTMLEditor.HTMLEditorImplementation.TinyMCE));



I found that while syntax highlighters are common on the Internet, there are not so many choices for desktop applications. Since Native Swing has simple mechanisms to integrate such web-based libraries, I decided to replace my Swing-based Java-only syntax highlighter to the SyntaxHighlighter library which supports many languages.

Now, to display some content with a given syntax highlight, it only takes a few lines:

JSyntaxHighlighter syntaxHighlighter = new JSyntaxHighlighter();
syntaxHighlighter.setContent(s, JSyntaxHighlighter.ContentLanguage.Java);

While I find it is not perfect, it is good enough. And the good news is that I can easily upgrade the library or replace it with another one while keeping the same API.


Native Integration only?

Native Swing was originally an effort to integrate native components, but the scope became broader: by simplifying the addition of web-based components, a whole component suite emerged. Native Swing now integrates native and web-based components.

The web browser is the door to many libraries which can nicely be integrated in Java applications. For developers and users, this is good news: we can create richer applications.

Native Swing is an on-going effort, so please let me know what you think. As usual, share your ideas!


Native Swing:
Webstart Demo:

Published at DZone with permission of its author, Christopher Deckers.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


B. Ertung replied on Fri, 2008/11/21 - 5:22am

Are Java applets supported by the JWebBrowser? If they are, can be access them using nsIWebBrowser? How is the Iframe support? is it possible to access the individual Iframes?

How many platforms do you support? Windows, mac, linux, solaris?

These are excellent components if they are stable.

Christopher Deckers replied on Fri, 2008/11/21 - 5:43am in response to: B. Ertung

Last time I checked, Java applets were not supported. This may change in the future, because I think Sun will want to fix that when they decided to embed a web browser as well.

DOM support through XPCOM should not be a problem, and accessing it through Javascript is possible too.

Windows and Linux are supported. Solaris may be, I never tried. Mac is not yet supported because I bridge SWT which does not allow to be embedded in a Swing application. The next official version of SWT should allow it though, so I have high hopes of getting that platform supported in the future.

Don't hesitate to check the WebStart demo and give your feedback. I'm listening to user needs, so I am quite open to suggestions.



romil choksi replied on Mon, 2008/12/01 - 5:14am

how can i add login functionality in my java web browser project?it ll be great if u provide me the code for it.pls reply

Christopher Deckers replied on Mon, 2008/12/01 - 7:55am in response to: romil choksi


I provide a framework to integrate, among other things, a web browser.

Having a login form in a web or Java application is a standard development problem and is not related to the scope of this project as far as I can see.

For this type of help, you should consider asking your question on dedicated developer forums.

Hope this helps,


hookfi john replied on Sun, 2009/05/31 - 7:58am

cheap tiffany with.Discounted Tiffany & Co silver jewelries are provided in our Tiffany’s online outlet store. Give Tiffany Earringsto your love. We have a wide range of Tiffany Necklacesfor you to choose from.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.