How can I put a Swing GUI on a C++ application?

Alexander Krapf

The easiest way to do that is to use the JunC++ion product. JunC++ion generates C++ proxy classes for compiled Java classes and allows you to use the generated C++ classes just as you would use the underlying Java classes.

In order to put a Swing GUI on a C++ application you have to decide in which language you want to do most of the work. You can either create most of the GUI in Java and then proxy these classes to C++, or you can build the Swing GUI in C++ by assembling custom dialogs or windows from proxy components (I personally prefer the first way).

One crucial problem that JunC++ion solves very neatly is the information flow between the Java GUI and the C++ application. JunC++ion allows you to implement Java Listeners in C++. This allows you to receive event notifications when the user presses an OK button or selects a menuitem and to perform the required application logic in C++.

A very simple example of a C++ application that has a Java GUI is below. This little application just shows a custom Swing window and waits for the window to go away before terminating. It also shows how easy it is to add a new button to the GUI from C++. JunC++ion allows more elegant solutions for the "keep-alive" problem than polling, but they require more space.

#include "DemoPanel.h"
#include "javax_swing_pkg.h"

int main()
  DemoPanel  p( "Test Window" );

  p.setBounds( 10, 10, 200, 200 );
  p.add( JButton( "New Button" ) );

  p.setVisible( true );

  while( p.isShowing() )
    Sleep( 500 );

  return 0;
JunC++ion is currently generally available on Wintel but ports to various Unixes are underway. You can find out more about JunC++ion at http://www.codemesh.com