Step 9.4: Write the Code for the HelloSWT Class


Step 9.4: Write the Code for the HelloSWT Class

click to expand

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.

Option 1: Source Entry

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.

click to expand
Figure 9.14: Source code entered into the editor pane.

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.

click to expand
Figure 9.15: Using the scrollbar to 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.

click to expand
Figure 9.16: Using the Save tool to save the source.

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).

click to expand
Figure 9.17: The workbench after saving the new HelloSWT source.

Option 2: Importing Source from CD

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.

click to expand
Figure 9.18: Select File/Import ... to Import a file into the SWT project.

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.

click to expand
Figure 9.19: Select File system as the source for the import.

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.

click to expand
Figure 9.20: Enter R:\Source\Step 9, where R is the drive where you loaded the included CD-ROM.
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.

click to expand
Figure 9.21: Select the Step 9 folder by left-clicking on it.

This will cause the contents of the Step 9 folder to appear in the right-hand pane, as shown in Figure 9.22.

click to expand
Figure 9.22: The contents of the Step 9 folder will appear in the right hand pane.

Select HelloSWT.java by clicking on its checkbox.

q 9.4(i) Select HelloSWT.java and click Finish.

click to expand
Figure 9.23: Select only HelloSWT.java, then click Finish.

You should see the following dialog box.

q 9.4(j) Select Yes on the confirmation box that pops up.

click to expand
Figure 9.24: This prompt confirms that you really want to overwrite your source.

Your display should look just like the one shown in Figure 9.25.

click to expand
Figure 9.25: The workbench display after importing the source code. Note the red X's in the Package Explorer.

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.




Eclipse
Eclipse: Step by Step (Step-by-Step series)
ISBN: 1583470441
EAN: 2147483647
Year: 2003
Pages: 90
Authors: Joe Pluta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net