【问题标题】:Drop Down ComboBox [closed]下拉组合框[关闭]
【发布时间】:2016-10-11 21:35:16
【问题描述】:

我的 ComboBox 有许多字符串对象,例如(“David”、“John”、“Mary”、“Gabriel”、“Anderson”、“Henry”、“Johnson”、“Halstead”、“Annie” ,“禧年”)。 组合框是可编辑的。 所以,无论我在组合框中写什么,在下拉选项中,我都应该只获得与组合框中键入的字符串匹配的字符串对象。 假设,如果我在 Combobox 字段中输入了“An”,那么 ComboBox 列表中应该只有 Annie 和 Anderson。A

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

}

【问题讨论】:

  • 但问题是什么?
  • 我投票结束这个问题作为离题,因为它是代码编写请求,没有任何问题描述阻止 OP 自己编写它。

标签: java swing


【解决方案1】:

这与 AutoCompleteComboBox 不同。

也许,但概念是一样的。

每次在组合框中输入一个字符时,你都会做一些事情。在这种情况下,您要做的就是搜索条目列表并仅将有效的条目添加到组合框的模型中。

所以你需要在组合框的编辑器中添加一个DocumentListener。然后,每当触发 DocumentEvent 时,您都会从组合框的编辑器中获取当前值。然后清除组合框中的所有项目。最后,您搜索项目列表并将匹配的项目添加到组合框中。

编辑:

要访问用作您刚刚使用的编辑器的文本字段:

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

【讨论】:

  • 请看上面的代码...我不能将keyListener添加到comboBox的字段(此处为inputField)吗?
  • @ShahrozSaleem 建议使用DocumentListener,而不是KeyListener。无需扩展组合框即可直接访问文本字段。见编辑。
猜你喜欢
  • 1970-01-01
  • 2010-11-30
  • 2014-08-13
  • 2010-10-27
  • 2011-04-13
  • 2015-09-14
  • 1970-01-01
  • 2013-02-21
相关资源
最近更新 更多