Technique behind the Auto complete

I was tired of finding the technique behind the Auto complete that can be used with text-box. Now I don’t let you to get those exhausts as I made my own auto complete program and  I break the  auto-complete code to see what is just behind the sense.

Auto complete

Auto complete

Standard I used:  AutocompletejTextField1 –> name of the textbox

AutocompletejTextField1KeyReleased –> this is the function I used to invoke auto   complete task (i.e. auto complete codes are written inside this function)

This function will be called automatically whenever key is released in your keyboard

Key release: an action that does not take place at the time when key is pressed but at the time when same key is released

Auto complete task is going to accompanied with the help of database here

For demo purposed create a database (with name) –>  country

Table name –>country_Table

Field name –> country_names

I used java to accompanies these task

What will be result : When u press “A” inside the text box , Afghanistan will automatically  appear in the text box .if suppose you continued typing i.e. after “a” image you press “u” then in your text box  Australia will appear. Further continued typing matching country in the database will appear automatically in the text box

private void AutocompletejTextField1KeyReleased(java.awt.event.KeyEvent evt)

{

String actualtext; // I am just creating string variable

/* I am getting the content in the text box and assigning it to actualtext */

actualtext = AutocompletejTextField1.getText();

/*I am getting the length of text box and assigning to the integer variable*/

int select = AutocompletejTextField1.getText().length();

/* I am checking whether  the key released is backspace or some other key.*/

boolean isBackspace = evt.getKeyChar() == ‘u0008’;

/*It is advisable to take a look of else part first then come back to if part for better   Understandment */

if (isBackspace)

{

/* when backspace key is pressed selected text should be deleted and exact  matching world at  the                           moment   should be retrieved from the database and then it should be displayed in text box. */

/*help to get the contents  before backspace  key is pressed*/

Actualtext = actualtext.substring(0, select – 1);

AutocompletejTextField1.setText(actualtext.substring(0, select – 1));

try {

String sql, sql2, sql3;

sql = ” select country from country_Table where country_names like ‘”;

sql2 = “%'”;

sql3 = sql.concat(actualtext.concat(sql2));

res = stat.executeQuery(sql3);

res.next(); //a recode will be select from the table

/*I am assigning the selected record to the text box */

AutocompletejTextField1.setText(res.getString(1));

/* integer select will determine the number of letters that too be selected in the   textbox */

AutocompletejTextField1.setSelectionStart(select-1);

}

catch (SQLException ex) {

// Your exception code

}

} // if closing

else {

// key released is not a backspace key then auto-complete task start here

try {

String sql , sql2, sql3;

sql = “select country from country_Table where country_names like ‘”;

sql2 = “%'”;

sql3 = sql.concat(actualtext.concat(sql2));

/* for example if a key ‘A’ is released then sql statement would be like this select    country from                                 country_Table where country_names like ‘A’*/

res = stat.executeQuery(sql3);

res.next(); // a recode will be select from the table

/* I am assigning the selected record to the text box */

AutocompletejTextField1.setText(res.getString(1));

/* integer select will determine the number of letters that too be selected in the textbox*/

AutocompletejTextField1.setSelectionStart(select);

} catch (SQLException ex) {

// your exception code

}

} // else close

} // key release function closing


4 Comments

  1. Lakshmi wrote
    at 2:06 PM - 27th January 2010 Permalink

    hi
    auto complete code was really good even though i don’t know any think about program I am telling this…..:)

    But there is something written on the picture i tried to do that ,nothing is happening why ?

    Then wh about the smart card program i asked u to post… did u forgot ?

    keep updating me ….

  2. shakthydoss wrote
    at 2:18 PM - 27th January 2010 Permalink

    Sorry yeah …it was a gif picture but actions are not working that lead you to confusion .Next I dint forget about request (smart card prog) I will try to post without any further delay.
    Your really active in all my posts thank u very mush

  3. Shashank wrote
    at 3:01 PM - 27th January 2010 Permalink

    very well written and contemplated article…
    looking for more from you..

  4. Haja Peer Mohamed H wrote
    at 3:44 PM - 23rd September 2010 Permalink

    Great Post… Great Blog. Keep it up! Nice beginning!

Post a Comment

Your email is never published nor shared. Required fields are marked *