Saturday, June 28, 2008

AutoComplete in Java

While using Internet Explorer, we use autocomplete facility in browsers, google search for e.g. if you type 'g' in the browser, all the urls starting with 'g' will appear below.

If you are using java and you want the same thing in a textfield, for e.g. you want that whenever you write anything in the textfield, it should show every value those are starting with the letter you type, meaning, you want autocomplete in textfield, here is the trick.

In java, there are APIs available to do that effectively.

Suppose jTextField1 is the textfield in which you want to have that autocomplete facility. Here is the code to do it.

Demo Code

AutoComplete au = new AutoComplete(jTextField1);

au.add("One");

au.add("Two");

au.add("Three");

au.add("Four");


au.add("Five");

au.add("Six");

au.add("Seven");

au.add("Eight");

au.add("Nine");

au.add("Ten");

You can find the API in below mentioned links:

http://femur.biostr.washington.edu/~mindseer/doc/edu/washington/biostr/sig/brainj3d/shwing/swing/AutoComplete.html



It needs a specific jar file. You can find it in below link:

http://sig.biostr.washington.edu/projects/MindSeer/download.html

Practical Use

Suppose, you are using database. And you need the details based on a particular field or column which may be your primary key. You can add these primary keys to the autocomplete and use it.





Total Pageviews