Section 4 Add a SWT Library
Returned directory
To create a SWT program in eclipse, you must set the Java build path (or classpath) to include the swt jar file (SWT. Jar ). Let's write a simple SWT window program to verify that the environment required to generate the SWT program has been correctly set.
In the package Explorer window, right-click the project name created earlier and choose new> class. In the class name field, enter "blancerwindow" and click "finish. This class is created, and its code appears in the editor window. Enter the code in the List 2-1 in the edit window.
List
2-1: blankwindow. Java
Import org. Eclipse. SWT. Widgets .*;
Public class blankwindow
{
Public static void main (string [] ARGs)
{
Display display = New Display ();
Shell shell = new shell (Display );
Shell. open ();
While (! Shell. isdisposed ())
{
If (! Display. readanddispatch ())
{
Display. Sleep ();
}
}
Display. Dispose ();
}
}
Save the file and you will see some error messages in the task window. To compile this program, add the JAR file of SWT to the Java build path. In pakcage explorer, right-click the project name and select properties. In the displayed Properties window, you can configure build path. On the left-side pane, select Java build paht and libraries on the right. Click "add external jars" and add SWT. jar in the select file dialog box. Generally, the SWT. Jar path is:
<Eclipse_install_directory>/plugins/org. Eclipse. SWT. <windowing_system >_< eclipse_version_number>/WS/<windowing_system>/SWT. Jar
For example, in windows, eclipse3.0's SWT. the jar file is under the eclipse installation directory plugins \ Org. eclipse. SWT. win32_3.0.0 \ ws \ Win32; on the Linux Motif Platform, it is in plugins/org. eclipse. SWT. motif_3.0.0/WS/Motif; on Mac OS X, it is in plugins/org. eclipse. SWT. carbon_3.0.0/WS/carbon. Figure 2-11 shows the Java generation path of the added swt jar file.
Figure 2-11 Java build path
Tip:GTK also needs to add the swt-pi.jar to build path, where the swt-pi.jar is in the same directory as SWT. jar.
Now, there should be no error prompts in the task window. You have successfully saved and compiled the first program. However, you need to set a parameter before running this program. Because SWT implements Windows functions based on Java JNI (Java Native Interface), you must set the runtime environment ), so that it can find the local implementation in the library file. This setting is related to your operating system.
In the main menu of Eclipse, select Run-> run to open a run dialog box to set the local database. Click "new" to createBlankwindowThe main class of the configuration item is blamkwindow. Click the "parameter (argguments)" tab and enter the parameter in "VM Parameters ".-Djava. Library. PathAnd enter the following directory: the directory is generally <eclipse_install_directory>/plugins/org. Eclipse. SWT. <eclipwing_system >_< eclipse_version_number>/OS/<operating_system>/<processor_architecture>. In Windows, the plug-ins \ org In the eclipse installation directory. eclipse. SWT. win32_3.0.0 \ OS \ Win32 \ x86; for Mac OS X, it is plugins/org. eclipse. SWT. carbon_3.0.0/OS/MacOSX/PPC. In Windows, the path is 2-12.
Figure 2-12 "run" dialog box after SWT library is added
Click "run". A blank window is displayed. Next, you can build more meaningful SWT and jface programs.
Returned directory