DJ NativeSwing - reloaded: JWebBrowser, JFlashPlayer, JVLCPlayer, JHTMLEditor.
Join the DZone community and get the full member experience.
Join For FreeToday's release of DJ Native Swing 0.9.4 greatly improves stability and brings new components: in addition to the JWebBrowser and JFlashPlayer, there is now the JVLCPlayer and the JHTMLEditor.
Here is a summary of what to expect when using this library.
1. Various native components
DJ Native Swing was designed to handle all the complexity of native integration, mainly in the form of components with a simple Swing-like API.
Here are some screenshots of several components in action (click to enlarge):
JWebBrowser: | JFlashPlayer: |
JVLCPlayer: | JHTMLEditor: |
2. Simple API
First, we need to initialize the framework. This needs to happen before any feature is used. A common place for this call is the first line of the main():
public static void main(String[] args) {
NativeInterfaceHandler.init();
// Here goes the rest of the initialization.
}
Now, let's see how to create a JWebBrowser, with its URL set to Google's homepage:
JWebBrowser webBrowser = new JWebBrowser();
webBrowser.setURL("http://www.google.com");
myContentPane.add(webBrowser);
Note that we set a URL, but we could as well set the HTML text. The JWebBrowser also allows to execute Javascript calls, and we can even propagate notifications from custom pages to our Swing application.
Moving to a more practical example, we may want to be notified of URL change events or track window opening events, potentially preventing navigation to occur or open the page elsewhere. This is easily achieved by attaching a listener:
webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
public void urlChanging(WebBrowserNavigationEvent e) {
String newURL = e.getNewURL();
if(newURL.startsWith("http://www.microsoft.com/")) {
// Prevent the navigation to happen.
e.consume();
} else {
// We can consume the event and decide to open this page in a tab.
}
}
public void windowWillOpen(WebBrowserWindowWillOpenEvent e) {
// We can prevent, add the URL to a tab, etc.
}
// There are of course more events that can be received.
});
Let's have a look at the JFlashPlayer:
JFlashPlayer flashPlayer = new JFlashPlayer();
flashPlayer.setURL(myFlashURL);
The JFlashPlayer can open local or remote Flash files, and files from the classpath; the latter being a general capability of the library by proxying files using a minimalistic web server.
Of course, the JFlashPlayer allows to retrieve and set Flash variables, and play/pause/stop the execution.
The JVLCPlayer and the JHTMLEditor are no exceptions to this simplicity: playlist can be manipulated in the VLC player, HTML can be set and retrieved from the HTML editor, etc.
There is also the possibility to integrate Ole controls on Windows, still with a simple Swing-like API. An example is provided in the library in the form of an embedded Windows Media Player.
3. Advanced capabilities
The library takes care of most common integration issues. This covers modal dialog handling, Z-ordering, heavyweight/lightweight mix (to a certain extent), invisible native components with regards to focus handling and threading.
Here are some more screenshots, showing a lightweight/heavyweight mix, and Z-ordering capability:
The demo application that is part of the distribution shows all the features along with the source code, so check it out!
4. Project info and technical notes
Webstart demo: http://djproject.sourceforge.net/ns/DJNativeSwingDemo.jnlp
Screenshots: http://djproject.sourceforge.net/ns/screenshots
Native Swing: http://djproject.sourceforge.net/ns
The DJ Project: http://djproject.sourceforge.net
The 0.9.4 version has a completely new architecture. It still uses SWT under the hood, but it does not use the SWT_AWT bridge anymore.
The JWebBrowser and browser-based components require XULRunner to be installed, except on Windows when using Internet Explorer.
The JVLCPlayer requires VLC to be installed.
The JHTMLEditor uses the FCKeditor.
5. Conclusions
This project finally brings all that is needed to make Java on the desktop a reality. A web browser, a flash player, a multimedia player, and even an HTML editor. So, what next?
Yes, what next? For that one, I am waiting for your feedback. So, what do you think? What are your comments and suggestions?
-Christopher
Published at DZone with permission of Christopher Deckers. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments