As usual, you can do this one of two ways: write the code or import the code. Option 1 is to modify the source manually. If you'd rather skip this step, go on to Option 2 to import the code.
Enter the new code.
q 9.8(a) Enter the following source code after the comments.
import java.util.Hashtable; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class Prompt { // Resources Display display; Shell shell; Color red, white; // Fields Text f1; Label f2; public Prompt() { } public void run() { init(); setLayout(); createWidgets(); show() ; cleanup(); } private void init() { // Create a standard window display = new Display(); shell = new Shell(display); shell.setText("Prompt3"); // Create some colors red = new Color(display, 255, 0, 0); white = new Color(display, 255, 255, 255); } private void setLayout() { // Create the layout for the widgets GridLayout grid = new GridLayout(); grid.numColumns = 2; grid.makeColumnsEqualWidth = true; shell.setLayout(grid); } private void createWidgets () { // Create my widgets Label l1 = new Label(shell, SWT.NONE); l1.setText("Item Number:"); f1 = new Text(shell, SWT.BORDER SWT.SINGLE); f1.setTextLimit(20); Label l2 = new Label(shell, SWT.NONE); l2.setText("Description:"); f2 = new Label(shell, SWT.NONE); f2.setText(" "); Button bl = new Button(shell, SWT.PUSH); b1.setText("Find"); b1.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { doFind(); } }); Button b2 = new Button(shell, SWT.PUSH); b2.setText("Exit"); b2.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { doExit() ; } }); } private void doFind() { String desc = getDescription(f1.getText()); if (desc == null) { f1.setForeground(white); f1.setBackground(red); f2.setText("Not Found"); f2.setForeground(red); } else { f1.setForeground(null); f1.setBackground(null); f2.setText(desc); f2.setForeground(null); } } private void doExit() { shell.close() ; } private static final Hashtable items; static { items = new Hashtable(); items.put("DOG", "My Puppy"); items.put("CAT", "My Kitty"); items.put("OCTOPUS", "Calamari Kid"); } private String getDescription(String item) { return (String) items.get(item); } private void show() { // Final setup - size the display show it shell.setSize(200, 100); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } private void cleanup() { // When done, clean up resources display.dispose(); red.dispose(); white.dispose(); } public static void main(String[] args) { new Prompt().run(); System.exit(0); } }
q 9.8(b) Save the source code.
q 9.8(c) Press Ctrl-Home.
Your display should now be similar to Figure 9.55.
q 9.8(d) Insert the supplied CD-ROM into your CD-ROM drive.
q 9.8(e) Right-click on the SWT project and select Import ...
q 9.8(f) Select File system and click Next.
q 9.8(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.
q 9.8(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.60.
Select Prompt.java by clicking on its checkbox.
q 9.8(i) Select Prompt.java and click Finish.
If you also executed Option 1, you'll get the following dialog box. Click Yes and skip to Step 9.9.
q 9.8(j) Select Yes on the confirmation box that pops up.
If you didn't execute Option 1, your display should look just like the one shown in Figure 9.63.
q 9.8(k) Open Prompt.java.
Your display should now look like Figure 9.64.