1

I am looking for a solution for my problem. I use NetBeans7 and I am trying use JComboBox with setEditable(true). I need that user enter with one word at a time on the JcomboBox and a SELECT LIKE is executed and returning one List with all results found for the JCombo.

I did see this: Set Size of JComboBox PopupMenu

But I can not implement.

here how to I am trying.

//the model
public class JComboModelFuncoes extends AbstractListModel implements ComboBoxModel{  
    private Object selectedItem;  
    private List<Funcoes> listaFuncoes = null;  

    public JComboModelFuncoes(List<Funcoes> lista){  
        listaFuncoes = new ArrayList<Funcoes>();  
        listaFuncoes.addAll(lista);  
    }  

    @Override  
    public int getSize() {          
        return listaFuncoes.size();  
    }  

    @Override  
    public Object getElementAt(int index) {  
        return listaFuncoes.get(index);  
    }  

    @Override  
    public void setSelectedItem(Object anItem) {  
        selectedItem = anItem;  
    }  

    @Override  
    public Object getSelectedItem() {  
        return selectedItem;  
    }  

}  


//here JComboBox 
comboPesquisa.setMaximumRowCount(10);
    final JTextField tf;    
    tf = (JTextField)comboPesquisa.getEditor().getEditorComponent();
    tf.setDocument(new LimitaNroCaracteres(50));
    tf.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e){ 
            comboPesquisa.removeAllItems();
            List<Funcoes> lista = new FuncoesDAO().retornaFuncao(tf.getText());    
            for(Funcoes f : lista){
                comboPesquisa.addItem(f.getFuncao());
            }
            comboPesquisa.setPopupVisible(true);
        }      
    });

//here my DAO that return all results
public List<Funcoes> retornaFuncao(String funcao){
         List<Funcoes> lista = new ArrayList<Funcoes>();
         PreparedStatement stm = null;
         ResultSet rs = null;
         try{
             stm = this.con.prepareStatement("SELECT * FROM funcoes WHERE funcao LIKE ?");
             stm.setString(1, "%" + funcao + "%");
             rs = stm.executeQuery();
             while(rs.next()){
                 Funcoes f = new Funcoes();
                 f.setIdFuncao(rs.getLong("id_funcao"));
                 f.setFuncao(rs.getString("funcao"));
                 lista.add(f);
             }             
         }catch (SQLException e){
             JOptionPane.showMessageDialog(null, "Erro tentando consultar função", "Erro", JOptionPane.ERROR_MESSAGE);
         }finally{
            try {
                rs.close();
                stm.close(); 
            } catch (SQLException ex) {
                Logger.getLogger(FuncoesDAO.class.getName()).log(Level.SEVERE, null, ex);
            }

         }
         return lista;
     }

How to make this work ?

thanks

Community
  • 1
  • 1

1 Answers1

3

What you're looking for is usually called "autocomplete". Here is an implementation of one:

Autocomplete JComboBox

Paul
  • 19,704
  • 14
  • 78
  • 96