The Swing/AWT API over SWT library SwingWT Project Page


Q. Is this project dead?

A. Yes, I'm afraid so. I no longer need it. Hopefully it's still useful to someone.

Q. Couldn't you have used the java.awt.*/javax.swing.* package names? I have to search/replace all my code before I can use this stuff!

A. Yes and No. Yes, technically it can be done, but it requires deployment to jre/lib/endorsed for javax.* packages (making it tougher to deploy apps). Other disadvantages are that the default Sun classloader will refuse to load anything starting with java.* (so you need to override the classloader), and the big worry that classes might get merged from the real AWT/Swing.

You can also use various Java pre-processors or Ant to change your code at build time for you.

If you are using a free virtual machine (such as Kaffe, SableVM, GIJ or IKVM), the SwingWT build script allows you to build it with java.awt/javax.swing packages by specifying "ant -Dsun=no" at build time. You can then use the -bootclasspath parameter to load SwingWT prior to your VM's class libraries to have it override any existing AWT/Swing implementation.

If you are a Win32 developer, the ThisIsCool MingW GCJ project supplies you a complete GCJ toolchain for Windows using SwingWT as the AWT/Swing implementation - just compile your code with that and go!

SwingWT now includes a classloader (borrowed from the WebOnSwing project at http://webonswing.sf.net ) which will dynamically switch Swing for SwingWT on existing programs. See the swingwtbootstrap and linux_replaceswing_runswingdemo.sh scripts in the bin directory for a working example with a swing demo that uses SwingWT at runtime.

Q. Why didn't you implement this as Swing UI delegates?

A. I have to admit to not being enough of an expert to know if this is possible for sure. As long as you can abstract away the rendering of a component as part of an L&F (which I assume you can), then yes, it's possible.

The reason I didn't even consider it is that I wanted this for building apps with GCJ (which lacks a complete Swing implementation) - I therefore had to assume that I had nothing and was basically reimplementing Swing from scratch.

This does have the other rather nice side-effect that SWT is currently making waves in the mobile device world, and using SwingWT means you can port your Swing apps to mobile devices easily!

Q. Can I use SwingWT to use a Swing like API over SWT containers (Eclipse plugins)?

A. Yes you can - although SwingWT closely resembles the Swing/AWT APIs, you can access the SWT widgets with a call to getSWTPeer() for any component and getComposite() for any container. This works two ways, and you can wrap up an existing SWT widget with a SwingWT one.

To add a SwingWT container (eg: JPanel) to an SWT container, simply create the SWT container, then create a new swingwt.awt.Container object and assign the SWT container to it. You can then treat it like any other Swing container. Eg:

   // If this is an Eclipse plugin, tell SwingWT to use the
   // already existing SWT event dispatcher instead of creating
   // it's own:

   // Create a new SWT shell to use as a container
   // -------
   Shell shell = new Shell();
   shell.setLayout(new org.eclipse.swt.layout.FillLayout());

   // Map it to a SwingWT container so we can use our Swing-like 
   // components with it
   // -------
   swingwt.awt.Container container = new swingwt.awt.Container();
   container.composite = shell;

   // Add components to it as normal
   // -------

Q. The browser demo isn't working on my Linux box! I get an SWT error thrown saying "No More Handles"

A. The SWT browser kit requires the following:

  1. Mozilla 1.4 - 1.7 with GTK2/XFT and GRE support
  2. The environment variable LD_LIBRARY_PATH must include the mozilla installation directory (eg: /usr/lib/mozilla).
  3. The environment variable MOZILLA_FIVE_HOME must be set to the mozilla installation directory (eg: /usr/lib/mozilla)

Q. Is SwingWT compatible with third party AWT/Swing layout managers?

A. Yes (from v0.77), as long as you have the source for the layout manager you want to use and you can change the java.awt imports to swingwt.awt and recompile it.

Q. How can I prevent the copyright message for SwingWT being dumped to stdout at startup?

A. Before you do anything with SwingWT in your code, do: swingwtx.swing.SwingWTUtils.setShowSwingWTInfoOnStartup(false) to turn it off.

Q. Feature X is missing/I want to implement feature X - can you help?

A. That's great! However please don't do the following:

Q. How do I build SwingWT with GCJ?

A Makefile for GCJ is supplied in the root folder of the SwingWT distribution. Note that you will need the "unzip" and GNU find utilities (and of course GCJ) for it to work.

Win32 users can either use Cygwin to run a modified version of the Linux build script with MingW, or use the MingW build at ThisIsCool for a pre-built SwingWT/SWT.

Q. I can't use SwingWT for my application because it's not "free enough"

A. I assume you are talking about the CPL. With SWT:

  1. Nobody can take the code off you and prevent you from using it
  2. You can modify and redistribute the code as you like
  3. You can redistribute binary packages with your applications
  4. Your app doesn't become CPL just because you are using SWT

That's good enough for me. SwingWT is LGPLd so it's super-free :-)

Q. The SwingWT demos work fine, but my applications don't work on MacOS X?

A. The Java threading model is fundamentally broken in MacOSX and it requires the SWT event loop to run on the main thread (SwingWT spawns a new dispatch thread on other platforms).

You can get around this by:

  1. In the main method for your program, call initialiseMacOSX() with your startup code called from a new Runnable.

          public static void main(String[] args) {
              if (SwingWTUtils.isMacOSX()) {
                  SwingWTUtils.initialiseMacOSX(new Runnable() {
                      public void run() {

    This code will work with other platforms since it just calls the startup code as normal if you are not running under Mac OS X.
  2. Use the java_swt application in SwingWT/lib/macosx_carbon instead regular old java to start your app.

    See SwingWT/bin/macosx_rundemo.sh for an example.


Copyright(c)2003-2012, R. Rawson-Tetley