Over a million developers have joined DZone.

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

DZone's Guide to

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

· ·
Free Resource

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: http://djproject.sourceforge.net/ns
Screenshots: http://djproject.sourceforge.net/ns/screenshots
Webstart Demo: http://djproject.sourceforge.net/ns/DJNativeSwing-SWTDemo.jnlp


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}