2016-06-10 93 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