0

I need an editable JComboBox that every time I type a letter in it, searches in the database all Strings like the String written and also it automatically expands the combobox each time I type a letter with the new results from the query.

I have tried several listeners, but can't make them work propperly, this is what i have so far.

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {  
         try{
                String typedString = jComboBox3.getEditor().getItem().toString();

                jComboBox3.showPopup();

                DefaultComboBoxModel model = new DefaultComboBoxModel();
                System.out.println(typedString); 
                    try {
                        String query = "SELECT Client.Surname, Client.Sex, Client.CodeClient FROM Client WHERE Client.Surname LIKE '"+typedString+"%' ORDER BY Client.Surname;";
                        bd.getConn();
                        bd.statement=bd.conn.prepareStatement(query);
                        bd.rs=(ResultSet)bd.statement.executeQuery(query);
                        while(bd.rs.next()){
                            String client = bd.rs.getString("Client.Surname");
                            String sex = bd.rs.getString("Client.Sex");
                            String codClient = bd.rs.getString("Client.CodeClient");
                            switch(sex){
                                case "M":   model.addElement("Mr. "+bd.rs.getString("Client.Surname"));
                                            break;
                                case "F":   model.addElement("Ms. "+bd.rs.getString("Client.Surname"));
                                            break;
                                default:    model.addElement(bd.rs.getString("Client.Surname"));
                                            break;
                            }

                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(Registro.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    jComboBox3.setModel(model);
                    if (jComboBox3.getItemCount() > 0) {
                        jComboBox3.getEditor().setItem(typedString);
                        jComboBox3.showPopup();                     

                    } else {
                        jComboBox3.addItem(typedString);
                    }
            }catch   (IllegalComponentStateException e) {
                return;
            }
}
  • You probably need a `JSpinner`. https://docs.oracle.com/javase/7/docs/api/javax/swing/JSpinner.html – dryairship Jan 26 '16 at 13:21
  • Possible duplicate question? http://stackoverflow.com/questions/8949466/detecting-jcombobox-editing – hamena314 Jan 26 '16 at 13:49
  • Also you've said, that you tried several listeners, did you try the `DocumentListener` in particular? Because the `ActionListener` usually only fires if the Return- / Enter-key is pressed IIRC. – hamena314 Jan 26 '16 at 13:52
  • [this](http://stackoverflow.com/questions/26550559/how-to-create-a-search-bar-similar-to-google-search-style-in-java-gui) may help – guleryuz Jan 26 '16 at 18:42
  • @guleryuz Hey Really Thanks, It Works, but, there may be some bug , or I need a specific code, because i can't backspace , I have the same problem as this guy : [link](http://stackoverflow.com/questions/17391362/java-allow-using-backspace-in-an-editable-jcombobox-with-substance-lf) but this line : "UIManager.put(LafWidget.COMBO_BOX_NO_AUTOCOMPLETION, Boolean.TRUE);" won't work for me because I'm not using -> LafWidget – Jesús Suárez Borrell Jan 27 '16 at 11:48

0 Answers0