January 28, 2021
Hot Topics:

SWT Programming with Eclipse

  • By Koray Guclu
  • Send Email »
  • More Articles »

5. Running SWT Applications Outside of Eclipse

To run the application without using Eclipse, the swt.jar library must be in your classpath, and the java.library.path enviroment variable must be set properly. Depending on the host platform, the appropriate native library file must be available. For the Windows platform, you can do the following to make the native library configuration for your application:

  1. Put swt.dll in the same directory as the program.
  2. Put swt.dll in the JAVA_HOME\bin\ directory.
  3. Put swt.dll in the c:\windows\system32 directory.

javac classpath c:\swt\swt.jar HelloWorld.java
Java classpath c:\swt\swt.jar;. Djava.library.path=c:\swt HelloWorld
The java.library.path is the required environment variable for the JNI. If you don't set this environment, your DLL class is not accessible. In that case, the application cannot run properly and throws an exception.

SWT libraries
Swt libraries are available under the Eclipse plug-in directory. If you want to get the SWT libraries without downloading the whole Eclipse package, it is possible to download a single SWT library under the http://www.eclipse.org/downloads directory.

6. SWT Packages

The SWT consists of the following main packages. Definitions of the packages are taken from the Eclipse API documentation. You can find the API documentation on the Eclipse Web site.
org.eclipse.swt: contains classes that define constants and exceptions that are used by SWT classes. An SWT package consists of three classes: SWT, SWTException and, SWTError. The SWT will probably be your favorite class because it contains constants for SWT libraries such as keyboard, error, color, layout, text style, button etc. constants.
org.eclipse.swt.widgets: Contains most core SWT widget components, including the support interface and classes.
org.eclipse.swt.events: Defines typed events, listeners and events, that SWT components use. This package contains three different groups of classes: Listener interfaces, Adapter classes, and Event class.
org.eclipse.swt.dnd: Contains classes for drag-and-drop support of the SWT widgets.
org.eclipse.swt.layout: Contains classes providing automated positioning and sizing of the SWT widgets.
org.eclipse.swt.print: Contains classes providing print support for the SWT widgets.
org.eclipse.swt.graphics: Package provides the classes which implement points, rectangles, regions, colors, cursors, fonts, graphics contexts (that is, GCs) where most of the primitive drawing operations are implemented, and images including both the code for displaying them and the public API for loading/saving them.

7. Dialogs

Dialog implementations are native. That is to say, dialogs, like widgets, are platform components. Dialogs in SWT are derived from the Dialog abstract class. A dialog is not a widget but it can contain widgets.

Figure 2. Dialog class hierharcy.

There are different types of dialogs available. Some dialogs can have specific properties. A a dialog can be used as shown in Source 2.

Source 2. MessageBox example
MessageBox messageBox =   new MessageBox(shell, SWT.OK|SWT.CANCEL);if (messageBox.open() == SWT.OK){  System.out.println("Ok is pressed.");}

Each dialog's open() method returns different types. For instance, the MessageBox dialog returns int from the open() metod. Therefore, one must write different conditions to handle the return value for each dialog.

ColorDialog shows a color selection pallet. It returns an RGB object from return method.

DirectoryDialog enables you to select a directory. It returns A String from THE open() method. The returning value is the selected directory. It is also possible to set additional filters to filter the directories.

The Font dialog enables a user to select a font from all available fonts in the system. It returns a FontData object from the open() method.

FileDialog enables a user to select a file. Additionally, you can set an extension filter, path filter, and filename filters. This dialog has the styles shown in Table 1:

Table 1. SWT Dialog style bit constants
SWT.OPEN Shows Open button in the dialog
SWT.SAVE Shows Save button in the dialog

PrintDialog enables a user to select a printer before starting a print job. It returns a Printer Data object from the open() method.

The MessageBox dialog is used to give feedback to the user. You can combine different styles by the | operation as shown in Source 3.

Source 3. MessageBox example
 MessageBox messageBox =   new MessageBox(shell,    SWT.OK|    SWT.CANCEL|    SWT.ICON_WARNING); messageBox.setMessage("www.korayguclu.de"); messageBox.open();

The resulting message box of the above example is shown in Figure 3.

Figure 3. MessageBox dialog.

Available button constants are listed below. A combination of different buttons can be made by using the | operation. The SWT framework builds the dialog depending on the style bits. The button constants are: SWT.ABORT, SWT.OK, SWT.CANCEL, SWT.RETRY, SWT.IGNORE, SWT.YES, and SWT.NO.

Table 2 shows a list of available icons to be used by dialogs.

Table 2. SWT icon style bit constants

Page 2 of 6

This article was originally published on March 25, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date