How to Create an Ajax Autocomplete Text Field: Part 2 / Page 2 | WebReference

How to Create an Ajax Autocomplete Text Field: Part 2 / Page 2

[previous] [next]

How to Create an Ajax Autocomplete Text Field: Part 2 [con't]

Creating an Autocomplete Text Field Using the Eclipse Web Tools Platform

To create the Fund class, right click on the "Java Resources" branch in the "Project Explorer" pane, just as you did to create the package, and hover the mouse pointer over the "New" popup, but this time select the "Class" item from the second popup (See Figure 8).

Click on "Class" to bring up the "New Java Class" dialog. There are a number of fields on this dialog, but we only need to use the "Package" and "Name" fields. Click the "Browse" button to launch the "Package Selection" dialog and select the "com.webreference.autocomplete" package (See Figure 9)

Enter "Fund" in the "Name" textbox (See Figure 10).

Add the Private Variables

After you close the dialog, the empty Fund class will open in an editor in the center pane, ready for coding. We'll begin with the private member variables because in this type of class, they are the basis for everything else. Type or copy and paste the following code into the class:

Immediately, two errors will appear. To see what the problem is, hover the mouse pointer over the first red "X" on the left. A tool tip will appear stating that "BigDecimal cannot be resolved to a type." It can't be resolved to to a type because the BigDecimal is not a native data type, such as a String or double (See Figure 11)

To remove the error, you'll have to import it. Normally, you would have to look through the Java Sun docs to locate where the BigDecimal class resides, but Eclipse saves you the trouble. It offers a few ways to import a package into the file, such as clicking on the "X" to bring up a popup menu. There you will find several methods of dealing with the error, including creating a new class or passing to the class as a constructor argument. Double-click on the first item to import it (See Figure 12).

Another way is to right click anywhere in the code to bring up a popup menu. Hovering over the "Source" entry brings up a second popup that includes the "Add Import" command (See Figure 13).

Alternatively, you can simply type "Ctrl+Shift+M" to use the keyboard shortcut. Follow the same procedure to import the GregorianCalendar.

Your errors have now disappeared, only to be replaced by four warnings! Hovering the mouse over any of the warnings will reveal a tool tip stating that "The field Fund.<fieldname> is not used locally." This is an importantl message that can remind you to remove variables that would up being unused. We can ignore it since we will be using them soon enough (See Figure 14).

Generate a Constructor

We can now use the fields to generate a constructor. Follow the same procedure as we did above and right-click on the source code to bring up the context menu. Once again, hover over the "Source" entry to display the second popup. This time, select the "Generate Constructor using Fields..." item to bring up a dialog box (See Figure 15).

By default, all the fields are included in the constructor, which is precisely what we want. Choose "First Method" as the insertion point so the constructor is generated at the top of the class. Check the "Omit call to default constructor super()" because there is no initialization in the default Object class (See Figure 16).

Click "OK" to see the new constructor:

[previous] [next]