Embedding Swing components inside WPF and WinForms applications

You may be aware that you can use JNBridgePro to embed Java UI elements inside Windows Presentation Foundation (WPF) and Windows Forms (WinForms) applications. However, all our examples are built around Java’s Abstract Windowing Toolkit (AWT) framework, and there might be questions as to whether Swing is supported, too. The answer is “Yes!” Let’s do an example to show how easy it is.

We’ll start with our existing example showing how an AWT component can be embedded inside a WPF application, and then show how easy it is to convert the example to use Swing instead of heavyweight AWT.

After you’ve downloaded the example code and document, go ahead and work through the example. It shouldn’t take long. [Don’t worry; we’ll wait!]

Are you back?  Great.  Now, let’s make a quick change to the embedded Java component, converting all the AWT elements to Swing.  It should look something like this:

package jInN;

import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JavaComponent extends JPanel {

     public JTextField javaTextBox;
     JButton btn;

     public JavaComponent() {
          setSize(400, 100);
          JLabel label = new JLabel("Java: ");
          javaTextBox = new JTextField(30);
          btn = new JButton("SEND");

     public void addActionListener(ActionListener l)

Note how all each AWT element (Panel, Label, Button, TextField) has been changed to the corresponding Swing element (JPanel, JLabel, JButton, JTextField).  Rebuild the JavaComponent class, and reproxy it, making to include all supporting classes.

Now, modify the .NET code (particularly, the file Window1.xaml.cs).  Change all instances of java.awt.TextField to javax.swing.JTextField:

private javax.swing.JTextField javaTextBox;

public DotNetActionListener(javax.swing.JTextField jTB, TextBox dNTB)
     javaTextBox = jTB;
     wpfTextBox = dNTB;

Everything else can stay the same.

Now, rebuild your .NET code, and run the application.  Where before you saw an AWT component embedded in the WPF application, now you’ll see a Swing component:

Embedding Swing in WPF

It’s that simple!  Just write and build your Swing component, and proxy it along with supporting classes. Then, on the .NET side, wrap the proxy inside a com.jnbridge.embedding.JavaWPFControl (supplied by us as part of the JNBridgePro API) and use it wherever you’d use a WPF UIElement. If you’re building a Windows Forms application rather than a WPF application, then wrap the Swing component’s proxy in a com.jnbridge.embedding.JavaControl instead, and use it wherever you’d use a WinForms UserControl.

Note that the Java 7/8 UI embedding issues still hold here, so if your Swing component is complex (that is, it’s a hierarchy of subcomponents) and makes use of focus-based events like mouse wheel and keyboard events, then you should use Java 6 in your application to avoid these issues. Otherwise, Java 7 or 8 will work just fine.

Are you embedding Swing elements in .NET applications? Are there any other Java UI technologies that you’d like to embed in .NET applications that you’d like to see us support? Contact us and let us know.