Photo by KNXRT on Unsplash

In a previous article I spoke about some features and use cases of JNet. The example in the article shown a basic example to use an AWT User Interface from a .NET console application.

However .NET, especially on Windows, is not only console, it supports graphics too. In this article I’m going to discuss about the integration of some .NET and JVM UI Frameworks.

Why someone need this feature?

The answer to the question can be found in some daily work. The reader could answer: I don’t need this feature, I’ll create the UI using the framework I love. However this is not possible in all conditions you can found at work. I try to explain better with some use cases:

  • My customer have a ready made AWT/Swing based UI, however the evolution of the project needs to use .NET. I can save time and money integrating them and avoiding the porting of AWT/Swing UI.
  • My customer has a .NET based UI and I have ready made AWT/Swing UI components. I can integrate my components in the customer UI with little effort. The same can be done if the customer has a Java based UI and I have ready made .NET UI components.
  • I have to manage the integration of components from multiple stakeholders: each stakeholder can use its preferred technology, no problem in integration.

I listed some usages of this feature, however many other possible cases can come from real world.

How to integrate both UI frameworks?

The integration happens behind the scene, the user needs to give few information to JNet and everything is done.

I’m entering better in the code to show something.

The glue

The real glue between both UIs is behind the scene: the user just need to use the CreateHostedContainer method available in each class extending an AWT Container class.

A snippet like the following shows how to connect both UI worlds:

MyJavaUIToBeIntegrated javaUserUI = new MyJavaUIToBeIntegrated(); Container javaContainer; object netUIComponent; javaContainer = new Panel(); javaContainer.Add(javaUserUI); #if WINFORMS netUIComponent = javaContainer.CreateHostedContainer(false); #else netUIComponent = javaContainer.CreateHostedContainer(true); #endif 

The steps are few and simple. The objects involved are:

  1. The class MyJavaUIToBeIntegrated represents the UI interface written in Java. javaUserUI represents an instance of MyJavaUIToBeIntegrated. MyJavaUIToBeIntegrated can be any UI element: from a pre-made UI available in your JAR, or a programmatically written UI; in the test examples, or templates, of the JNet repository the UI is made programmatically;
  2. The variable javaContainer represents the Container of the Java UI will connect both UI worlds;
  3. The variable netUIComponent represents the .NET graphical object (WPF Control or WinForms Control) to be inserted into .NET UI layout.

The UI integration needs only 3 steps:

  1. Build the javaContainer using a Panel: Panel inherits from Container and has its own characteristic. The user can choose any kind of class, the main requirement is that javaContainer shall be a Container or shall have Container has superclass.
  2. Insert javaUserUI within the Panel instance*:* executed in .NET, but the final stage happens within the JVM like any other UI integration can be made in Java.
  3. Call CreateHostedContainer to obtain the object usable from .NET for graphical integration;
  4. Insert netUIComponent within the .NET UI layout like any other WPF/WinForms Control.

The previous steps create only the graphical integration and both UIs runs alone (message pump are isolated). To integrate them you can use method invocation (from .NET to JVM) and callbacks (from JVM to .NET).

The simple example result

As mentioned before, within the JNet repository, there are some examples of UI integration; here some screenshot of the results obtained running the examples:

WPF application hosting AWT graphic

The above picture shows a simple AWT layout (TextArea + Button) nested into a WPF application. Pressing the AWT Button, the WPF hosting application receives a trigger, reads the content of the TextArea and put the value in the corresponding TextBox of the WPF application.

The following picture, instead, shows a Swing based UI integrated within the same .NET application of the previous example:

WPF application hosting Swing graphic

What’s next step?

I spoke about integration and shown the result. I used the simple examples found in the repository so anyone can reproduce them without problems. When the logic behind is well known the examples can be extended, or replaced, with your own graphic to verify the effectiveness of the approach. Attaching events on your AWT/Swing graphic objects you can fully integrate it with the hosting .NET application, or if you don’t need feedback from JVM, you can simply invokes methods on the JVM objects from .NET.

Everything depends on your needs.

Conclusion

In this article I discussed about the integration of the .NET and JVM graphics. It is the continuation and extension of the previous article where JNet was presented the first time. Both articles, this and the previous one, demonstrate how simple is the usage of JVM from .NET using JNet. I have reported some use cases, however there are other possible usage I can’t imagine 😉.