Java GUI implementation with Eclipse SWT, Eclipse Visual Editor Project and Maven: Part 3


This post will introduce you the end-to-end development with the Maven by using the very simple traditional, the Hello World ! So that you may apply to your own requirement.

The SWT Hello World !

Firstly we need the empty Maven project. All required dependency artifacts which have been mentioned at the previous post should be set at the project POM file as well.

After that we will create the SWT: Shell Visual Class at which you will see File –> New –> Other — > Java –> SWT –> Shell Visual Class. Here is the simple Hello World source which is generated by the visual editor. Please note, it is not a time to make a source code to be beautiful. We just need to see only the big picture.

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class HelloWorldSWT {

    private Shell sShell = null;
    private Text textName = null;
    private Label labelMessage = null;
    private Button buttonSubmit = null;
    private Button buttonCancel = null;
    private Label labelName = null;

     * This method initializes sShell
    private void createSShell() {
        final GridData gridData = new GridData();
        gridData.horizontalSpan = 2;
        final GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        this.sShell = new Shell();
        this.sShell.setSize(new Point(300, 200));
        this.labelName = new Label(this.sShell, SWT.NONE);
        this.textName = new Text(this.sShell, SWT.BORDER);
        this.labelMessage = new Label(this.sShell, SWT.NONE);
        this.labelMessage.setText("I'm waiting for you.");
        this.buttonSubmit = new Button(this.sShell, SWT.NONE);
                new {
                    static final String MESSAGE = "Hello ";
                    public void mouseDown(
                            final e) {
                        final String name = HelloWorldSWT.this.textName.getText();
                        HelloWorldSWT.this.labelMessage.setText(MESSAGE + name);
        this.buttonCancel = new Button(this.sShell, SWT.NONE);

            new {
                public void mouseDown(
                        final e) {

        Display display = null;
        display = Display.getDefault();
        while (!this.sShell.isDisposed()) {
            if (!display.readAndDispatch()) {

    public static void main(final String[] args) {
        final HelloWorldSWT main = new HelloWorldSWT();

The Unit Testing with SWTBot

Normally, we should test all possible scenarios such as the following:-

Table 1: Test case scenario

# Scenario Expected Result
1 When the user enter the name and click submit. The label should displays “Hello <NAME>”.
2 When the user enter the name as an empty String

and click submit.

The label should displays “Hello ”. (There is a space.)
3 When the user enter the name and click cancel. The text and label should be cleared.
Etc. Etc.

I will show you only one case, the case #1. It just create a simple JUnit4 class and put the SWTBot related statements as the following: –

import junit.framework.Assert;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.finders.ControlFinder;
import org.eclipse.swtbot.swt.finder.finders.MenuFinder;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotLabel;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.scc.biometric.poc.mock.swt.HelloWorldSWT;

public class HelloWorldSWTTest {
    private static final Logger LOGGER;
    static {
        LOGGER = Logger.getLogger(HelloWorldSWTTest.class);

    private SWTBot bot;

    public static void setUpBeforeClass() throws Exception {

    public static void tearDownAfterClass() throws Exception {

    public void setUp() throws Exception {

    public void tearDown() throws Exception {


    static {
        new Thread(new Runnable() {
            public void run() {
                try {
                    HelloWorldSWT.main(new String[] {});
                } catch (final Exception e) {
                          fatal("Cannot instantiate the Hello.", e);

    public void whenSay() {

    private void initSWTApplication() {

    private void waitForDisplayToAppear(final long time) {
        long    endTime = -1;
        Display display = null;
        try {
            endTime = System.currentTimeMillis() + time;
            while (System.currentTimeMillis() < endTime) { // wait until timeout
                try {
                    display = SWTUtils.display();
                    if (display != null) {
              "The display is found.");
                } catch (final Exception e) {
                          warn("Cannot find display, keep waiting.");
                try {
                } catch (final InterruptedException e) {
                    HelloWorldSWTTest.LOGGER.warn("The thread is interrupted", e);
                } // sleep for a while and try again
        } finally {
            display = null;

    private void initSWTBot() {
        ControlFinder cf = null;
        MenuFinder    mf = null;
        try {
            cf       = new ControlFinder();
            mf       = new MenuFinder();
   = new SWTBot(cf, mf);
        } finally {
            cf = null;
            mf = null;

    private void say(final String name) {
        SWTBotLabel  swtBotLabel  = null;
        SWTBotButton swtBotButton = null;
        SWTBotText   swtBotText   = null;
        String       saidMessage  = null;
        try {
            swtBotText   =
            Assert.assertNotNull("Cannot find the textName.", swtBotText);

            swtBotButton ="Submit");
            Assert.assertNotNull("Cannot find the buttonSubmit.", swtBotButton);

            saidMessage  = "Hello ";

            if (name != null) {
                saidMessage += name;
            } else {
                saidMessage += "null";

            swtBotLabel  =;

            Assert.assertNotNull("Cannot find the said lable.", swtBotLabel);

        } catch (final Exception e) {
            HelloWorldSWTTest.LOGGER.fatal("This is an unexpected error", e);
            throw new RuntimeException(e);
        } finally {
            swtBotLabel  = null;
            swtBotButton = null;
            swtBotText   = null;

Test It !!!

There are 2 way for performing the testing, first is either via the JUnit within the Eclipse IDE or via the command line as “mvn test”.


We have created the big picture, end-to-end classic traditional Hello World SWT application, including with the unit testing with SWTBot inside the Maven environment. The next step is fulfilling the test case so that it covers all the possible scenarios as a good practice.

Since we always be the Maven, then our application can be expanded to the other infrastructure more simply and easily. e.g Maven report, Maven Site, Code quality, Code Coverage, Continuous Integration server and so on. Most of them not only support the Maven project and but also provide120% compatible and cross platform.

Please take note, I’m on the Windows x86 Vista and develop the Maven project. My infrastructure is on the Linux: CentOS x86_64. The CI Server can build and perform the unit testing successfully. I have achieved the cross platform already.

About Charlee Chitsuk

I've been working as a software developer since 1998. At the moment I focus on system integration including with SOA and Enterprise Security Solution.

Posted on April 11, 2011, in Java GUI, JUnit, Maven, SWT, SWTBot, Unit Testing, Visual Editor and tagged , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: