Wednesday, May 2, 2012

Getting Started with SWT and JFace

Setting up a development environment for SWT/JFace can be pretty difficult because most of the libraries needed are not available in Maven repository. Even if they are available in Maven repository, they are not always up to date. In this blog, I'm going to show you how to setup a development environment for SWT and JFace. This example uses Gradle and Eclipse 3.7

The easiest way to get the libraries needed for SWT/JFace is to copy those libraries from Eclipse plugins directory. For SWT, there are only two libraries required, the SWT library and the SWT native platform library. For JFace, we need the JFace libraries, Workbench libraries, Equinox library, and Command library. The example of build.gradle is shown below.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    flatDir { dir 'lib' }
}

dependencies {
    // swt dependencies
    compile name: "org.eclipse.swt_3.7.2.v3740f"
    compile name: "org.eclipse.swt.gtk.linux.x86_3.7.2.v3740f"

    // jface dependencies
    compile name: "org.eclipse.jface_3.7.0.v20110928-1505"
    compile name: "org.eclipse.jface.text_3.7.2.v20111213-1208"
    compile name: "org.eclipse.jface.databinding_1.5.0.I20100907-0800"
    compile name: "org.eclipse.ui.workbench_3.7.1.v20120104-1859"
    compile name: "org.eclipse.ui.workbench.texteditor_3.7.0.v20110928-1504"
    compile name: "org.eclipse.equinox.common_3.6.0.v20110523"
    compile name: "org.eclipse.core.commands_3.6.0.I20110111-0800"
}
SWTApp.java
package swtproject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTApp {

    public static void show() {
        Display display = new Display();
        Shell shell = new Shell(display);

        Text helloText = new Text(shell, SWT.CENTER);
        helloText.setText("Hello SWT");
        helloText.pack();

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
JFaceApp.java
package swtproject;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

public class JFaceApp extends ApplicationWindow {

    public JFaceApp() {
        super(null);
    }

    protected Control createContents(Composite parent) {
        Text helloText = new Text(parent, SWT.CENTER);
        helloText.setText("Hello JFace");
        parent.pack();
        return parent;
    };
    
    public static void show() {
        JFaceApp app = new JFaceApp();
        app.setBlockOnOpen(true);
        app.open();
        Display.getCurrent().dispose();
    }
}
Main.java
package swtproject;

public class Main {

    public static void main(String[] args) {
        SWTApp.show();
        JFaceApp.show();
    }
}

No comments:

Post a Comment