2016-06-10 112 views
-5

我的組合框有很多字符串對象讓我們說一個實例(「David」,「John」,「Mary」,「Gabriel」,「Anderson」,「Henry」,「Johnson」,「Halstead」 「安妮」,「銀禧」)。 組合框是可編輯的。 所以,無論我在comboBox中寫入,在下拉選項中,我應該只獲取與在comboBox中鍵入的字符串匹配的字符串對象。 比方說,如果我在Combobox字段中輸入了「An」,那麼ComboBox列表中應該只有Annie和Anderson。下拉組合框

這與AutoCompleteComboBox不同。所以,請不要歡迎這樣的答案。

public void setEditor(ComboBoxEditor editor){ 
    super.setEditor(editor); 
    setEditable(true); 
    if (editor.getEditorComponent() instanceof JTextField) { 
     inputField = (JTextField) editor.getEditorComponent(); 
     inputField.addKeyListener(new KeyAdapter() { 
     public void keyTyped(KeyEvent e){ 
      char key = e.getKeyChar(); 
      String[] matchedString; 
      if(Character.isLetterOrDigit(key)||Character.isSpaceChar(key)||key=='\b'){ 
       if(key=='\b'){ 
        matchedString = getMatchedItems(inputField.getText()); 
        removeAllItems(); 
        for(int i=0; i<matchedString.length; i++){ 
         addItem(matchedString[i]); 
        } 
       } 


       else{ 
        matchedString = getMatchedItems(inputField.getText()+key); 
        removeAllItems(); 
         for(int i=0; i<matchedString.length; i++){ 
          addItem(matchedString[i]); 
         } 
       } 
      } 

     } 

     private int getMatchedCount(String currentWord) { 
      int n = getItemCount(),count=0; 
      for(int i=0; i<n; i++){ 
       if(((String)getItemAt(i)).toLowerCase().startsWith(currentWord.toLowerCase())){ 
        count++; 
       } 
      } 
      return count; 
     } 

     private String[] getMatchedItems(String currentWord){ 
      int n = getItemCount(),k=0; 
      String[] matchedString = new String[getMatchedCount(currentWord)]; 
      for(int i=0;i<n;i++){ 
       if(((String)getItemAt(i)).toLowerCase().startsWith(currentWord.toLowerCase())){ 
        matchedString[k++] = (String)getItemAt(i); 
       } 
      } 
      return matchedString; 
     } 
    }); 
    } 
} 

public static void main(String[] args) { 
    JFrame fr=new JFrame(); 
    fr.setLayout(null); 
    /*List <String> list = new ArrayList<String>(); 
    list.add("Shahroz"); 
    list.add("Wasif"); 
    list.add("Akram"); 
    */ 
    String str[] = {"Shahroz","saleem","Khan","Wasif","Dutta","Piyush","Rajat","Rehan","Rajesh"}; 
    fr.add(new AutoCombo(str)); 
    fr.setSize(400, 800); 
    fr.setVisible(true); 
} 

}

+1

但問題是什麼? – Pshemo

+2

我正在投票結束這個問題作爲題外話題,因爲它是代碼寫入請求,沒有任何描述停止OP寫它自己的問題的描述。 – Pshemo

回答

2

這是從AutoCompleteComboBox不同。

也許,但概念是相同的。

每次在組合框中鍵入一個字符,你都會做一些事情。在這種情況下,你所做的是搜索條目列表,並只將有效的元素添加到組合框的模型中。

因此,您需要在組合框的編輯器中添加DocumentListener。然後,無論何時啓動DocumentEvent,您都會從組合框的編輯器中獲取當前值。然後清除組合框中的所有項目。最後,您搜索項目列表並將匹配的項目添加到組合框中。

編輯:

要訪問使用你只需要使用編輯文本字段:

ComboBoxEditor editor = combobox.getEditor(); 
JTextField textField = (JTextField)editor.getEditorComponent(); 
// add the DocumentListener to the Document 
+0

請參閱上面的代碼...我不能將keyListener添加到comboBox(inputField here)的字段中? –

+0

@ShahrozSaleem建議使用「DocumentListener」,而不是KeyListener。不需要擴展組合框,您可以直接訪問文本字段。請參閱編輯。 – camickr