Results 1 to 3 of 3
Thread: AutoComplete for jtextfield
- 02-12-2009, 06:17 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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.
- 02-12-2009, 06:41 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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
- 02-12-2009, 06:46 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
how to use live validation with autocomplete in dojo text boxes in <s:text box>
By subashm28 in forum Suggestions & FeedbackReplies: 2Last Post: 01-23-2009, 04:09 PM -
Got problem with JtextField
By hungleon88 in forum AWT / SwingReplies: 4Last Post: 12-06-2008, 03:01 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks