How to create a google like drop down in java using a jcombobox
I'm having trouble crating a google like drop down list in java using a jcombo.
(I mean when I press 's' all the results starting with 's' will be displayed.)
by the way I've created a jcombo to work like that but it seems a little unstable.:confused:.It works fine for some time and gets stuck after typing in the search keys and deleting a few keystrokes.
Please help me solve this.
here goes my code.
final JTextComponent tcA = (JTextComponent) jComboBox1.getEditor().getEditorComponent();
tcA.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
//System.out.println("insert");
String s = null;
try {
//s = (String) jComboBox1.getEditor().getItem();
s = e.getDocument().getText(0, e.getDocument().getLength());
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
System.out.println(s);
ArrayList ar = new CustomerDAO().getCustomerNames(s);
//System.out.println(ar.get(i));
//jComboBox1.removeAll();
int howMany = ar.size();
final String[] placeArray;
if (howMany > 0) {
placeArray = new String[howMany];
ar.toArray(placeArray);
final JComboBox b = jComboBox1;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("inside invoke later");
//jComboBox1.setSelectedItem(s);
if (!b.isPopupVisible()) {
b.setModel(new DefaultComboBoxModel(placeArray));
b.setPopupVisible(true);
//b.setSelectedItem(s);
} else {
b.setPopupVisible(false);
b.setModel(new DefaultComboBoxModel(placeArray));
b.setPopupVisible(true);
}
}
});
}
thanks in advance.