JComboBox自动完成

时间:2012-12-03 11:07:23

标签: java swing autocomplete jcombobox

如何在Netbeans 7.1中的可编辑JComboBox中执行自动完成功能,如VB dot net中的ComboBox。我有一个带有列表绑定的组合框,我想通过在列表中只输入项目的第一个字母来选择项目,例如,如果列表有kitten,那么当我输入{{1}时应该选择它}}

3 个答案:

答案 0 :(得分:44)

如果您想自己动手,可以按照this article中说明的步骤进行操作。

this.comboBox = new JComboBox(new Object[] { "Ester", "Jordi",
        "Jordina", "Jorge", "Sergi" });
AutoCompleteDecorator.decorate(this.comboBox);

答案 1 :(得分:0)

我开发了一个名为" AutoComboBox "的自定义摇摆JComboBox。在您通过分叉此gist

键入时自动完成

这是一个小型演示。

首先声明并初始化。 (如果您使用的是IDE,只需将Class拖放到JFrame或JDialog表单上)

AutoComboBox autoComboBox = new AutoComboBox();

接下来,设置项目列表。它需要一个String数组。您可以通过修改AutoComboBox类来更改类型。

String[] itemArray =  {"Malith", "John", "Jack" };
autoComboBox.setKeyWord(itemArray);

现在你有一个自动完成的JComboBox,其中有#34; Malith"," John"," Jack"作为项目!

以下是我开发的两个类

AutoComboBox.java

import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;


public class AutoComboBox extends JComboBox<Object> {

String keyWord[] = {"item1", "item2", "item3"};
Vector myVector = new Vector();

public AutoComboBox() {

    setModel(new DefaultComboBoxModel(myVector));
    setSelectedIndex(-1);
    setEditable(true);
    JTextField text = (JTextField) this.getEditor().getEditorComponent();
    text.setFocusable(true);
    text.setText("");
    text.addKeyListener(new ComboListener(this, myVector));
    setMyVector();
}

/**
 * set the item list of the AutoComboBox
 * @param keyWord an String array
 */
public void setKeyWord(String[] keyWord) {
    this.keyWord = keyWord;
    setMyVectorInitial();
}

private void setMyVector() {
    int a;
    for (a = 0; a < keyWord.length; a++) {
        myVector.add(keyWord[a]);
    }
}

private void setMyVectorInitial() {
    myVector.clear();
    int a;
    for (a = 0; a < keyWord.length; a++) {

        myVector.add(keyWord[a]);
    }
}

}

ComboListener.java

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import org.apache.commons.lang3.text.WordUtils;

public class ComboListener extends KeyAdapter
{
@SuppressWarnings("rawtypes")
JComboBox cbListener;
@SuppressWarnings("rawtypes")
Vector vector;

@SuppressWarnings("rawtypes")
public ComboListener(JComboBox cbListenerParam, Vector vectorParam)
{
    cbListener = cbListenerParam;
    vector = vectorParam;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public void keyReleased(KeyEvent key)
{
            // TODO Auto-generated method stub
            String text = ((JTextField)key.getSource()).getText();

            cbListener.setModel(new DefaultComboBoxModel(getFilteredList(text)));
            cbListener.setSelectedIndex(-1);
            ((JTextField)cbListener.getEditor().getEditorComponent()).setText(text);
            cbListener.showPopup();
}


@SuppressWarnings({ "rawtypes", "unchecked" })
public Vector getFilteredList(String text)
{
    Vector v = new Vector();
    for(int a = 0;a<vector.size();a++)
    {
        if(vector.get(a).toString().startsWith(text))
        {
            v.add(vector.get(a).toString());
        }
                    else if(vector.get(a).toString().startsWith(text.toLowerCase()))
        {
            v.add(vector.get(a).toString());
        }
                     else if(vector.get(a).toString().startsWith(text.toUpperCase()))
        {
            v.add(vector.get(a).toString());
        }
                     else if(vector.get(a).toString().startsWith(WordUtils.capitalizeFully(text)))
        {
            v.add(vector.get(a).toString());
        }
                     else if(vector.get(a).toString().startsWith(WordUtils.uncapitalize(text)))
        {
            v.add(vector.get(a).toString());
        }
    }
    return v;
}
}

答案 2 :(得分:-1)

另一个搜索JComboBox的酷库: http://www.jidesoft.com/products/oss.htm

你可以在这里下载jar: http://www.java2s.com/Code/JarDownload/jide/jide-oss-3.5.7.jar.zip

在项目中导入库之后,您只需:

JComboBox comboBox = ....;
ComboBoxSearchable searchable = new ComboBoxSearchable(comboBox);