Tech Guides‎ > ‎

Processing and SWT in Eclipse

I needed to do some basic user interface for a Processing sketch.
- But lets face it: Doing UI in Processing sucks big time

Why not just use SWT (Standard Widget Toolkit) and get combos buttons and other nice stuff? 
- I had a hard time finding anybody doing this, so this guide should help others.

Switch to Eclipse

Abandon the Processing IDE and switch to Eclipse. Your life will be easier. You will smile more. Women will ask you out.

When you download, you want the "Eclipse IDE for Java EE Developers"
You also need to install a JDK (Java Development Kit) from Oracle/Java.

Coding Processing in Eclipse

You need to import the basic Processing libraries to your Eclipse workspace.

Getting started with SWT in Eclipse

Setup Eclipse for working with SWT. 

SWT and Processing

There are several ways of combining the Processing PApplet and a SWT 
In this example, I have two windows. - A window for Processing and a window next to it with my UI controls.
Processing window - And SWT window

There are two classes in this example and
We launch and then SimpleProcessingExample open SwtUI with the .. SWT UI. Tadaa!

package dk.liljedahl.christian.apx;

import processing.core.*;

// Launch this, the Processing thingy. This will open the UI thingy.

public class SimpleProcessingExample extends PApplet{

    public void setup() {
        size(300, 300);
        // Instantiate the UI - Using "this" to make it possible for the UI to reference this
        SwtUI mySwtUI = new SwtUI(this);
        Thread mySwtUIThread = new Thread(mySwtUI);

        //Start the UI-threads
    // position is public, to make it accessible from SwtUI
    public int position = 30;
    public void draw() {

          ellipse(position, 150, 25, 25);

(ColorCoding done with: )

package dk.liljedahl.christian.apx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SwtUI implements Runnable {

    SimpleProcessingExample myParent; 

    public SwtUI(SimpleProcessingExample myPapa) {
        // we need to establish a reference to the Processing Sketch, to make it possible to talk to it
        this.myParent = myPapa;

    public void run() {

        Display display = new Display ();
        Shell shell = new Shell(display);

        // Some properties - Size and placement. To keep nicely sided next to the Processing window
        shell.setBounds(315, 0, 300, 300);
        shell.setText("Button Example");
        shell.setLayout(new RowLayout());

        // The two buttons
        Button button1 = new Button(shell, SWT.PUSH);
        button1.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                myParent.position = 30;
        Button button2 = new Button(shell, SWT.PUSH);
        button2.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                myParent.position = 270;
        // Default Swt-stuff
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        display.dispose ();

(ColorCoding done with: )

More about java and threads