DJ Native Swing 0.9.6: Integrate Native Components from Other Libraries
DJ Native Swing is a great way to get a web browser, Flash player, multimedia player or an HTML editor in a Swing application. But until recently, it was not possible to apply its advanced integration logic to foreign native components. This has changed with the latest release.
In release 0.9.6, the library was split in two: the framework library with all the integration logic, and its SWT-based implementation which offers the rich component suite. Let us have a look at how we can make use of the framework part.
For the sake of the example, let us consider a Canvas subclass, which is the standard component used to peer native components. Here is the code of this subclass:
private static class CCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g.drawString("Some native component", 20, 80);
}
}
If we add this canvas class to a JDesktopPane in some internal frames, here is the default behavior:

As we can see, Z-ordering is completely messed up.
Now, let us use the Native Swing framework to see the difference. First, we need to add the DJNativeSwing.jar to the classpath. Then we need to initialize the framework:
public static void main(String[] args) {
// Not necessary, this is according to taste.
UIUtils.setPreferredLookAndFeel();
// This is the actual Native Swing initialization
NativeSwing.initialize();
// Rest of the main method...
}
Finally, we create a Swing-like component that will contain our native component but that would be taken care by Native Swing:
private static class NSPanel extends JPanel {
private CCanvas nativeComponent;
public NSPanel(NSOption... options) {
super(new BorderLayout(0, 0));
nativeComponent = new CCanvas();
add(new NativeComponentWrapper(nativeComponent).createEmbeddableComponent(options), BorderLayout.CENTER);
}
// Add methods to act on the native component.
}
The options that are passed in the constructor allow to define different behaviors. In the case of a JInternalFrame, it is desirable at least to call the constructor with the following option (and another one is required if iconification is desired):
new NSPanel(NSComponentOptions.proxyComponentHierarchy())
And the result when we re-run our application is the following:

This decoupling from the SWT-based implementation (in other words, the isolation of the framework) has certain interesting effects: one can now start to think of all his favorite libraries (JDIC browser?, Some Native media player that has Java bindings?) in contexts where certain integration issues were show stoppers.
I hope these features will help improving the state of Java on the desktop, and I am waiting forward to hearing from your experiments with foreign libraries!
- Login or register to post comments
- 3483 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
Carl Antaki replied on Thu, 2008/08/28 - 6:53am
Christopher Deckers replied on Thu, 2008/08/28 - 6:59am
in response to: carcour
Hi Carl,
Launching the SWT file dialog is a piece of cake. The problem is handling its modality because it completely escapes the AWT view of modality.
Native Swing SWT-based implementation keeps track of AWT modality to apply it to SWT components but not the other way round. It may be possible to implement this but it is not so easy.
Cheers,
-Christopher
Carl Antaki replied on Thu, 2008/08/28 - 9:13am
Thank you Christopher.
Great job for your library.
Regarding the modality do you mean that the dialog will appear behind the Swing JFrameor is the hard part making sure the Dialog is always modal?
I would love to see File Dialog support in DJ Native Swing, many people including msyelf are sick with the default JFileChooser which isn't on par with the native File Dialog. I've asked this many times in the Java.net forums but I didn't get any real answers; it was supposed to be fixed for Java 7 but I'm not even sure it's still on the list.
Thanks,
Carl
Christopher Deckers replied on Thu, 2008/08/28 - 9:21am
in response to: carcour
Hi Carl,
I'll add that item to my TODO lis. I don't promise anything though ;)
Cheers,
-Christopher
Carl Antaki replied on Thu, 2008/08/28 - 9:32am
Christopher Deckers replied on Thu, 2008/08/28 - 4:13pm
in response to: carcour
Oups, I noticed I forgot to answer one of your questions.
> Regarding the modality do you mean that the dialog will appear behind the Swing JFrameor is the hard part making sure the Dialog is always modal?
The hard part is in making sure the dialog is always modal. If I remember correctly, it is always on top but not modal as AWT has its own modality handling. And this is for Windows, it could be different on other systems...
Cheers,
-Christopher
Christopher Deckers replied on Thu, 2008/08/28 - 4:17pm
in response to: carcour
Don't hesitate to contact me by e-mail directly if you have any questions.
Cheers,
-Christopher
apu_ replied on Fri, 2009/02/06 - 5:13am
I use DJ Native Swing 0.9.6 (JWebBrowser) to embed some web page to my app.
I add JWebBrowser in the following way
add(new NativeComponentWrapper(webBrowser).createEmbeddableComponent(NSComponentOptions.proxyComponentHierarchy()), BorderLayout.CENTER);
It is work when I run app as desktop app. But I need use this is applet and it isn’t work.
If just add JWebBrowser as
add(webBrowser);
It is works, but I have problem with Z-ordering. How can I use JWebBrowser in applet app?
Thanks, Ihor
Christopher Deckers replied on Fri, 2009/03/27 - 1:14pm
in response to: apu_
Hi,
I am not sure what your problem is, but maybe using a JApplet instead of Applet would solve the issue.
-Christopher
apu_ replied on Wed, 2009/06/10 - 6:53am
in response to: chrriis
apu_ replied on Wed, 2009/06/10 - 7:29am
Great job for your library.
But I have following issue with your library
I have panel which contain jwebbrowser. Then I put transparent background rectangle under this panel (in upper layer). And under this rectangle I’ve added JInternalFrame .
All components are visible under this rectangle but jwebbrowser is not. I’ve try all NSComponentOptions but this don’t help.
I’ve added source of sample example, if you will have time please look to it. It will be very helpful for me
------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Christopher Deckers replied on Wed, 2009/06/10 - 1:21pm
in response to: apu_
Hi,
Change the content of the main method from the TestL class with code like this:
This code constructs the component correctly: UI is initialized in the UI thread, the native component is properly added to its hierarchy, so at least something shows up. BUT, the atual content is not visible and unfortunately this is norma. Native components do not behave like any normal Swing component: they do not support alpha blending for example.
Nevertheless, there is something you can do: when you show the layer, you can force the native component to create a background buffer every second or so and stop this thread when you hide the layer. The code for the background buffer effect is shown in the demo under the "Additional Features" and is called "Pseudo Transparency".
Hope this helps!
-Christopher
apu_ replied on Fri, 2009/06/19 - 6:02am
in response to: chrriis
Thank you very much. This was very helpful for me.