Results 1 to 3 of 3
  1. #1
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default AutoComplete for jtextfield

    Hello I have application which has a textfield. This textfield is associated with a popup which shows suggestions(starting with enterd letter) after user enters a letter in it.Its like autocomplete for a textfield.the problem is if i enter a it shows all words starting with a if i press b it shows all words starting with ab n if press c and if theres no suggestion available the popup doesnt hide. the code is as shown below.Please help.This method is called on keypress of jtextfield.
    public void csearch(String letter)throws SQLException

    {
    String word=null;
    DefaultListModel model = new DefaultListModel();
    option=false;

    String SQL="Select cname from company";
    Statement stat1 = conn.createStatement();
    stat1.executeQuery(SQL);
    ResultSet rs = stat1.executeQuery(SQL);



    int i=0;
    while(rs.next())
    {
    word=rs.getString(1);
    word=word.trim();
    if(word.startsWith(letter))
    {

    model.addElement(word);
    i++;
    option=true;
    }

    }


    final JList l = new JList(model);
    l.setOpaque(true);
    l.addListSelectionListener(newListSelectionListene r()
    {public void valueChanged(ListSelectionEvent e)
    {
    if(l.getSelectedIndex()!=-1)
    {

    a.nametxt.setText((String)l.getSelectedValue());
    pop.hide();//it works fine for this
    }


    }
    });


    PopupFactory factory= PopupFactory.getSharedInstance();
    int x=a.nametxt.getX();
    int y=a.nametxt.getY()+a.nametxt.getHeight();

    pop = factory.getPopup(a.nametxt,l,x+5,y+30);



    if(option==true)
    {
    pop.show();
    }
    else {
    pop.hide();//here is the problem
    }

    stat1.close();
    conn.close();

    }
    }
    if you know better way for doing this please suggest.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Typical algorithms for auto completion take one of 2 approaches:

    1. Require the user to enter enough characters so that the list is manageable. This is typical if there is no a priori way of ordering results by liklihoods.

    2. Order results by liklihood, presenting only the top 'n'. This is what google does. If you type 'a' into the search field, it presents a managable list of the most likley results. (amazon is #1, FYI

  3. #3
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    what is the problem in my code according to you? This is not web based application. Not jsp. Please reply I really need solution for this.
    Last edited by pinks_70986; 02-12-2009 at 08:19 AM.

Similar Threads

  1. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  2. Replies: 2
    Last Post: 01-23-2009, 05:09 PM
  3. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 04:01 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 05:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •