Tutorial: Creating a simple GUI Application

 

We will start by developing a simple, standalone GUI application, whose UI model is based on the Standard Widget Toolkit (SWT).

For simplicity’s sake, our application is a frame containing an SWT label and button. When the user chooses the button, the program displays the number of button clicks.

We will use this opportunity to introduce you, step by step, to working with the Eclipse IDE.

 

 

Step 1: Creating a new Java project

Prerequisites

First, we need a Java project for our example. The only other resource we need is a Java class whose contents can be copied from the source code file (included at the end of this section) in step 3.

Procedure

1.       Choose File ® New ® Project ...

2.       In the wizard that appears, choose the project category Java (in the left frame) and the project type Java Project (in the right frame).

3.       Choose Next.

4.       In the Project name field, enter a name for your project. (We suggest the name Demo Project).

5.       Choose Finish.

Result

The Eclipse IDE adds an appropriate project directory to the Package Explorer  view. It also automatically adds a .JAR file (rt.jar) for the Java runtime as a source attachment to the new project.

A project directory is created at the local file system level. The classpath file, containing information about the path of the project, is stored there.

 

Step 2: Creating a Java class

Procedure

1.       Select the project you have just created, Demo Project.

2.       Choose New ® Class from the context menu.

3.       In the Package field, enter the package name demo.simple. In Name, enter the name of the class you want to create, SimpleUI.

4.       Uncheck the public static void main checkbox.

5.       Accept the other default settings and choose Finish.

Result

The Eclipse IDE automatically launches the first Java editor, which displays an empty class definition.

 

Step 3: Implementing the class

Simply add the class implementation from the source code file SimpleUI.txt to the body of the class.

 

 

 

Step 4: Specifying the project dependencies

If you are creating an SWT-based GUI application, your project needs to create a reference to the appropriate API.

Procedure

1.       Select the project name (for example, Demo Project).

2.       Choose Properties from the context menu.

3.       Choose the Java Build Path property.

4.       Choose the Libraries tab and then choose Add External JARs. Add the swt.jar file from the org.eclipse.swt package folder ( ...\eclipse\plugins\org.eclipse.swt\...).

 

 

Step 5: Adding imports

Until now, we have not concerned ourselves with the external classes that we need to import into the Java file. However, if you use the Import Assistant, Eclipse performs this task for you.

Procedure

1.       Position the cursor anywhere in the Java Editor and choose Source ® Organize Imports from the context menu.

Button and element, the two elements we are using the in the class, occur in two different APIs (AWT and SWT). For this reason, a dialog box appears where you can choose the elements you want. If you do not make a choice, the IDE imports all the suggested elements.

2.       Choose the element you want (org.eclipse.swt.widgets.Button or org.eclipse.swt.widgets.Label ) , then choose Next.

3.       Choose the other elements you want, then choose Finish.

Result

The following imports are generated in the editor, in our example:

 

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.Shell;

 

 

Step 6: Saving and compiling a Java class

Eclipse includes an Autobuild compiler, which compiles whenever you save a Java resource. That is, if you now save the contents of the editor (without changing any standard Workbench settings), the compiler is launched.

In order to save the editor contents, choose the Save icon  from the menu bar.

 

 

Step 7: Executing the application

Prerequisites

If you start a SWT based standalone UI application for the first time in Eclipse, you need to copy the swt-win32-<*>.dll (the full name depends on Eclipse version) from the Eclipse installation folder plugins\org.eclipse.swt\os\win32\x86) into the Windows DLL folder (e.q.  \Winnt\system32\ in the Windows NT).

Otherwise a runtime error will be thrown if you try to the execute your application.

 

Procedure

1.       Select the executing java class (SimpleUI.java) from your project.

2.       Choose Run button from the toolbar.

3.       Choose Run As ® Java Application as your launcher.

 

Result

You’ve done it!

Eclipse launches our standalone application and displays the following GUI: