Hasi007
Hasi007

Reputation: 146

Using JComboBox as a search box

Im using a JComboBox to search a query from a sql database. Here is my code.

private void srKeyTyped(java.awt.event.KeyEvent evt){
    sr.removeAllItems();
    String sch = ((JTextField)sr.getEditor().getEditorComponent()).getText();
    String schh = "SELECT * FROM tbl WHERE name LIKE '" + sch + "%';";
    search = conn.getQuery(schh);
    try {
        while (search.next()) {
            String item = search.getString("name");
            sr.addItem(item);
        }
    } catch (SQLException ex) {
        Logger.getLogger(dataprocess.class.getName()).log(Level.SEVERE, null, ex);
    }
    sr.setSelectedItem(null);
    sr.setPopupVisible(true);

    System.out.println(sch);
}

sr = JComboBox

But when i type a letter in combobox, it adds all the items in database. I came to know that System.out.println(sch); always gives an empty string. And as soon as i type a letter, the text field of combo box becomes empty(i cant type a word with two letters). How to fix this? Thank you.

Upvotes: 0

Views: 6286

Answers (3)

Hasi007
Hasi007

Reputation: 146

Got the solution. This code works fine.

  private void srKeyTyped(java.awt.event.KeyEvent evt){

    String sch = ((JTextField)sr.getEditor().getEditorComponent()).getText();
    String schh = "SELECT * FROM tbl WHERE name LIKE '" + sch + "%';";
    search = conn.getQuery(schh);
    sr.removeAllItems();
    try {
        while (search.next()) {
            String item = search.getString("name");
            sr.addItem(item);
        }
    } catch (SQLException ex) {
        Logger.getLogger(dataprocess.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(sch);
    sr.setSelectedItem(null);
    sr.setPopupVisible(true);
    ((JTextField)sr.getEditor().getEditorComponent()).setText(sch);
  }

Thanks to Skepi,Kleopatra, Guillaume Polet

Upvotes: 0

Alan Yackel
Alan Yackel

Reputation: 498

Use an ActionListener instead of the looking for the key press. When a combobox's selection is edited it will fire an ActionEvent when the editing is done.

When you get this part working, you should move this logic off to another thread and populate the combobox's items when it returns. Otherwise your UI will hang while the SQL query occurs.

Upvotes: 1

Skepi
Skepi

Reputation: 478

The reasons for your problems are the following:

  1. sch is always empty is because you are calling sr.removeAllItems(); before you call String sch = ((JTextField)sr.getEditor().getEditorComponent()).getText();. This means that the contents of the JComboBox is cleared (along with the selection) before you get what is selected.

    Solution: Call sr.removeAllItems();AFTER you have got the selected item.

  2. The combo box becomes empty because you call sr.setSelectedItem(null); at the end after you have repopulated it.

    Solution: If you want the entered text then sr.getEditor().setItem(scr);

Only and idea but try to enclose the contents of the method in an if statement and check if the Enter key is pressed. That way the method contents will only execute after the desired string is input and not EVERY time a key is pressed.

Upvotes: 1

Related Questions