2011-12-20 4 views
1

내 문제의 해결책을 찾고 있습니다. NetBeans7을 사용하고 setEditable (true)로 JComboBox를 사용하려고합니다. 그 사용자가 한 번에 한 단어 씩 JcomboBox에 입력해야하고 SELECT LIKE가 실행되고 JCombo에서 찾은 모든 결과가있는 목록 하나를 반환해야합니다. JComboBox에서 선택을 하시겠습니까?

나는이 봤어 : Set Size of JComboBox PopupMenu

을하지만 구현할 수 없습니다.

여기 어떻게 노력하고 있습니다.

//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; 
    } 

어떻게 만드시겠습니까?

감사

당신은 일반적으로 "자동 완성"이라고 찾고있는 무엇

답변

관련 문제