JComboBox autocomplete

坚强是说给别人听的谎言 提交于 2019-11-26 05:33:37

问题


How do I perform auto-complete in editable JComboBox in Netbeans 7.1 like in ComboBox in VB dot net. I have a combo box with a list binding, I want to select item by typing only some first letter of the item in the list for example if a list has kitten, then it should be chosen when I type ki.


回答1:


If you want to do this yourself, you can follow the steps explained in this article.

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



回答2:


I developed a custom swing JComboBox named "AutoComboBox" which auto completes as you type on it by forking this gist.

Here is an small demo.

First declare and initialize it. (If you are using an IDE, just drag and place the Class on to your JFrame or JDialog Form)

AutoComboBox autoComboBox = new AutoComboBox();

Next, set the item list. It takes an String array. You may change the type by modifying the AutoComboBox class.

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

Now you have an auto completing JComboBox which has "Malith", "John", "Jack" as items !

Here are the two Classes I developed,

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

}

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



回答3:


Another cool library that search through a JComboBox: http://www.jidesoft.com/products/oss.htm

you can download the jar here: http://www.java2s.com/Code/JarDownload/jide/jide-oss-3.5.7.jar.zip

After importing the library in your project all you have to do is:

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


来源:https://stackoverflow.com/questions/13681977/jcombobox-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!