DJ Native Swing 0.9.6: Integrate Native Components from Other Libraries
Join the DZone community and get the full member experience.
Join For FreeDJ 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!
Published at DZone with permission of Christopher Deckers. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Chaining API Requests With API Gateway
-
Automating the Migration From JS to TS for the ZK Framework
-
Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
-
Tech Hiring: Trends, Predictions, and Strategies for Success
Comments