You may either enter the code directly into the editor, or use the Import feature to import the source from the supplied CD-ROM. If you wish to enter the source yourself and get a feel for the source editor, use Option 1 below. Otherwise, skip ahead to Step 9.4(d), the instructions for Option 2.
q 9.4(a) Enter the following source code after the comments.
import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class HelloSWT { public static void main(String[] args) { // Create a standard window Display display = new Display(); Shell shell = new Shell(display); shell.setText("HelloSWT"); // Final setup - set size and show the window shell.setSize(150, 50); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } // When done, clean up resource and exit display.dispose(); } }
At this point, your screen will look like the one in Figure 9.14.
Use the scroll bar to reposition the source to the top of the code. You can also use standard editing keystrokes, such as Ctrl-Home.
q 9.4(b) Position the window to the top of the source.
Now you can save the source. One way is to use the Save tool on the main Eclipse tool bar. You can also use Ctrl-S or the editor popup menu.
q 9.4(c) Click on the Save button on the toolbar.
You'll see the screen shown in Figure 9.17. You may skip ahead now to Step 9.5 (you may also perform Option 2 if you'd like, but there's really no need to do both options).
Here you'll perform nearly the same tasks as in Step 7; the only difference is the name of the file you'll be importing and the project you'll be importing it into.
q 9.4(d) Insert the supplied CD-ROM into your CD-ROM drive.
q 9.4(e) Select the SWT project, then select File/Import ... from the main menu bar.
On the Import screen, you have many options as to your import source. Since your source is in a folder on the CD-ROM, you'll use the File system option.
q 9.4(f) Select File system and click Next.
You need to specify the location of the files. Specify R :\Source\Step 9, where R is the letter of the CD-ROM drive where you loaded the disk. In Figure 9.20, the CD is loaded in the D: drive.
Note | Be sure that the Into folder field has a value of SWT. If it doesn't, then you didn't have the SWT project selected when you began the Import process. To fix the problem, just key in SWT. |
q 9.4(g) Enter R :\Source\Step 9 in the From directory field, where R is the drive letter of your CD-ROM drive, and press the Tab key.
The Tab key will cause the left pane to be loaded with an icon for the Source folder. Select the Source folder by clicking on it.
q 9.4(h) Left-click on the Step 9 folder.
This will cause the contents of the Step 9 folder to appear in the right-hand pane, as shown in Figure 9.22.
Select HelloSWT.java by clicking on its checkbox.
q 9.4(i) Select HelloSWT.java and click Finish.
You should see the following dialog box.
q 9.4(j) Select Yes on the confirmation box that pops up.
Your display should look just like the one shown in Figure 9.25.
In the Package Explorer view, you should see small red boxes with X's in them overlying the icons for the SWT project, its default package, and the HelloSWT.java class. These indicate that there is at least one error in the HelloSWT.java class. Now you need to determine what is causing the error.