devxlogo

Build Faster with This Turnkey SWT Project Template

Build Faster with This Turnkey SWT Project Template

he Eclipse Standard Widget Toolkit (SWT) is growing in popularity, as evidenced by its use in applications like Azureus and RSSOwl. In this 10-minute solution, you will learn how to make use of an SWT project template that can greatly reduce the time required to build small and medium-sized applications. In particular, the template offers the following features:

  • A UI that Works Out of the Box?The project already has a MainWindow class that comes complete with a menubar, toolbar, client area, status bar, and about dialog. The Open toolbar button and File menu item have code that opens a file dialog box, and the File | Exit menu option closes the app. (See Figure 1.)
    Figure 1. The basic template application, awaiting your modifications, is shown.
  • Executables?The project has an Ant task that makes it trivial to build executable JARs, Windows .exe’s, and Windows installers.
  • Better Help?Context-sensitive SWT Javadocs are integrated into the template.
  • Preferences?The MainWindow saves its window size and location in a preferences file that your application can use.
  • TextFile?The project includes a class that makes reading and writing text files trivial.
  • Cursors?A default (arrow) cursor and a waiting (hourglass) cursor have been added to the MainWindow class.
  • Portable?You will be able to take your project to other computers and it will just work because the swt.jar and associated .dll are included in the project.

To give you an idea of the time savings you can expect to see using the template, consider a utility that I wrote recently. The application allows the user to select a directory for source code analysis. The application then parses all source code files in the directory and its sub-tree, and compiles information on the number of lines of code, comments, and white space for each file. From the time I began working on the application, to the time that the application was being installed on a colleague’s computer, 50 minutes had passed. Of this, just under 40 minutes were spent on the tree-walking and source code parsing logic. Only 10 minutes were spent working on the user interface, building the executables, and creating an installer?tasks that could easily have taken hours to complete without the template.

The specific environment used to develop the SWT Project Template was Eclipse 3.2 (which includes Ant), JDK 6.0 and Windows XP (however, the basic steps can be adapted for use on *NIX and MacOS systems). Optional, but recommended, components include Jigloo GUI Builder 3.9, Launch4j 3.0, and NSIS 2.0.


You want to quickly build an SWT application, complete with a native executable and installer.


Start with the SWT Project Template, make a couple of simple modifications, then point-and-click to start the builder.

Installation

  1. Download and extract the SWT Project Template.
  2. In Eclipse, File | Import…| Existing Projects Into Workspace | Next | Select root directory | Browse to the template project’s root directory | OK. Finish.
Author’s Note: A description of the most significant files and folders in the project template can be found at the end of this article.

Usage
To Start a New Project Based on the SWT Project Template:

  1. Right-click on the template project | Copy.
  2. Right-click in an empty area of the Package Explorer | Paste.
  3. Check the default location checkbox.
  4. Change the Project name to the name of your new project. Click OK. A copy of the template project will be created in the Package Explorer.
  5. In your new project, expand the src directory. Now, right-click on the package named change_this_pkg_name. Select Refactor | Rename… and change the package name to something appropriate for you project. For example, if your project name is MyApplication, make the package name com.mycompany.myapplication. Then click Preview | Continue | OK.
  6. To run your application, go to the Package Explorer and expand your (renamed) package, then right-click on MainWindow and select Run As | Java Application (note that it’s not run as SWT application).

To Add UI Controls to the Window:

  1. If you are using Jigloo to edit the GUI, right-click on MainWindow in the Package Explorer, then select Open With | Form Editor. If you are using a different GUI builder, follow its standard procedure for opening a visual class.
  2. Drag a UI control into the clientArea composite. This composite defaults to the Absolute (null) layout.

To Set the Status Bar Text:

  1. In any method of the MainWindow class, call setStatus(String).

To Change the Cursor:

  1. In any method of the MainWindow class, call setCursor(waitCursor) to display the system waiting cursor (typically an hour glass), or setCursor(defaultCursor) to display the default cursor (typically an arrow).

To Read or Write Text Files:

  1. To read the contents of a text file into a String, use
    String someString = TextFile.read("blah.txt");
  2. To write the contents of a String to a file, use
    TextFile.write("blah.txt", someString);

To Get Help for an SWT Class or Method:

  1. Hover the cursor over the class or method name.
    or
  2. Put the cursor in a class or method name, then type Shift-F2. Your browser will be directed to Eclipse’s online help.

To Access the Application Preferences:
The application preferences are stored in a Properties object called appSettings. The settings are loaded from appsettings.ini on startup. They are saved back to appsettings.ini when the user selects exit from the file menu.

  1. To set a preference, use appSettings.setProperty(“myProperty”, “my value”);
  2. To get a preference, use appSettings.getProperty(“myProperty”, “default value”);

To Create Executables and/or Installers:

  1. Double-click build.xml.
  2. Modify the execJarBaseName, orgName, and version properties (1, 2, and 4, as shown in Figure 2) as appropriate for your application. The mainClassFullName property (3) should already by correct, but if it is not, modify it now.
    Figure 2. Set your properties as needed in the build file.
  3. To build the executable JAR file, go to the Outline view for build.xml. Right-click on buildExecJar, select Run As | Ant Build. Assuming everything goes well, this should put an executable JAR in your project directory. Double-click it in Windows Explorer and verify that your application runs as expected.
  4. To build the Windows .exe file, you must first have Launch4j installed on your machine. The Ant script (build.xml) assumes that launch4j is installed at C:Program Fileslaunch4j. If that is not correct, modify the launch4j.dir property in build.xml accordingly. Now, go to the Outline view for build.xml and right-click on buildWinExe, select Run As | Ant Build. Assuming everything goes well, this should put a Windows .exe file in your project directory. Double-click it in Windows Explorer and verify that your application runs as expected.
  5. To build the Windows installer, you must first have NSIS installed on your machine. The Ant script (build.xml) assumes that NSIS is installed at C:Program FilesNSIS. If that is not correct, modify the makeNsis property in build.xml accordingly. Now, go to the Outline view for build.xml, and right-click on buildWinInstaller, select Run As | Ant Build. Assuming everything goes well, this should put an executable Windows installer in your project directory. Double-click it in Windows Explorer and verify that your application is installed on your computer. The app is available via the Start menu. The application can be uninstalled via the Windows Add/Remove Programs control panel.
    Author’s Note: buildWinInstaller is dependent on buildWinExe, which is dependent on buildExecJar. Therefore, if you choose to run buildWinInstaller before the executable JAR or the .exe files are created, Ant will automatically build the JAR and .exe for you.
  6. More Customization: Modify images in /src/images, /launch4j, and /nsis as you like to provide an appropriate look and feel for your app. You might find MyPng2Ico useful for converting .png files to .ico files. You should also feel free to modify build.xml, installertemplate.nsi, and licensetemplate.txt however you like. By the way, the text in licensetemplate.txt is a BSD license.
  7. Figure 3. A look at the Windows installer.

Project Files
Table 1 contains brief descriptions of important files and directories in the template project.

Table 1. Index of Template Contents

FilenameDescription
appsettings.iniApplication preferences (Java Properties format).
build.xmlAnt file used to automate the building of executable JARs, .exe’s, and installers.
/launch4jContains images and icons used by the launch4j native application launcher.
/libContains JAR files used by the application. The template includes swt.jar, which provides access to the SWT widgets.
/nsisContains resource files used by NSIS to create a Windows application installer. This includes splash images, an NSIS template script, and a license file.
/srcContains Java source files and application images
MainWindow.javaSource for the main window and user interface thread
TextFile.javaUtility class for reading and writing text files
SWTResourceManager.javaUtility class used by the Jigloo GUI builder
swt-win32-3235.dllSWT DLL for Windows

The SWT Project Template is guaranteed to get your small to medium-sized SWT application up and running quickly by providing lots of built-in, time-saving features such as a default user interface, easy text file access, arrow and hourglass cursors, application preferences, and executable and installer build tasks.

So what’s next? Download the SWT Project Template and try if for yourself. For example, you can build a simple text editor by dropping an SWT Text widget on the clientArea. Next, use TextFile.read() in openFileMenuItemWidgetSelected() to load the text file that the user selects. Similarly, you can add a saveFileMenuItemWidgetSelected and use TextFile.write() to save changes to the file. Then run the Ant buildWinInstaller task, which will build a single installer file. Finally, copy the installer to another computer, install the application, and then launch it to see just how easy it can be to build distributable SWT applications.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist