JComboBox autocomplete

后端 未结 3 644
小鲜肉
小鲜肉 2020-11-27 05:32

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

相关标签:
3条回答
  • 2020-11-27 05:45

    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;
    }
    }
    
    0 讨论(0)
  • 2020-11-27 05:52

    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);
    
    0 讨论(0)
  • 2020-11-27 05:57

    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);
    
    0 讨论(0)
提交回复
热议问题