Using JComboBox as a search box

前端 未结 3 1786
一生所求
一生所求 2020-12-22 11:47

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.remove         


        
3条回答
  •  轮回少年
    2020-12-22 12:15

    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

提交回复
热议问题