Archive for June, 2011

Java Swing Dynamic Combo Box (suggestion box)

I think we can all agree that it’s a great user experience to have dynamically updated and populated dropdowns for user input. Especially if the user has a large choice of selections, or there are many similar ones. Or even if the user is unsure of what to type. It is especially usefull for search suggestions. you can apply any logic to the dynamic display, not necessarily based on alphabetical or matching searches (although I don’t know how useful this would be…)

So here’s some code for creating a dynamically populated JComboBox (a la google search hints). It uses a fixed array of items for demo purposes, but you can simply replace the code in the updateModel(String in) method (of the combobox model) with your own database query or whatever you need to do to grab your data.

The basics are : 1 create a JComboBox like usual. Set the combobox model to be an instance of the SearchBoxModel. Finally add an Item listener (the same instance of SearchBoxModel) to the combobox.

Some workarounds in this are : the popup is hidden and then displayed whenever the list data changes to get around redraw problems when the popup list items expand or reduce when the popup is visible. It is not noticeable in this demo, but perhaps if your item list is extensive it may pose an issue. If you do have a problem, I’d like to hear from you :)

To do this, wherever you create your combo box, set it up as follows :

//create the combobox
JComboBox searchbox = new JCombobox();
//we need it to be editable!!
//create the model
SearchBoxModel sbm = new SearchBoxModel(searchbox);
//set the model on the combobox
//set the model as the item listener also

Below is the source code for the combo box model.

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxEditor;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

public class SearchBoxModel extends AbstractListModel 
                implements ComboBoxModel, KeyListener, ItemListener
    ArrayList<String> db = new ArrayList<String>();
    ArrayList<String> data = new ArrayList<String>();
    String selection;
    JComboBox cb;
    ComboBoxEditor cbe;
    int currPos = 0;

    public SearchBoxModel(JComboBox jcb)
        cb = jcb;
        cbe = jcb.getEditor();
//here we add the key listener to the text field that the combobox is wrapped around

//set up our "database" of items - in practice you will usuallu have a proper db.
        db.add("iron oxide (2+)");
        db.add("iron oxide (3+)");
        db.add("sodium chloride");
        db.add("aluminium chloride");

    public void updateModel(String in)
//lets find any items which start with the string the user typed, and add it to the popup list
//here you would usually get your items from a database, or some other storage...
        for(String s:db)

        super.fireContentsChanged(this, 0, data.size());

//this is a hack to get around redraw problems when changing the list length of the displayed popups
        if(data.size() != 0)

    public int getSize(){return data.size();}
    public Object getElementAt(int index){return data.get(index);}
    public void setSelectedItem(Object anItem)
                                 {selection = (String) anItem;}
    public Object getSelectedItem(){return selection;}
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e){}

    public void keyReleased(KeyEvent e)
        String str = cbe.getItem().toString();
        JTextField jtf = (JTextField)cbe.getEditorComponent();
        currPos = jtf.getCaretPosition();

        if(e.getKeyChar() == KeyEvent.CHAR_UNDEFINED)
            if(e.getKeyCode() != KeyEvent.VK_ENTER )
        else if(e.getKeyCode() == KeyEvent.VK_ENTER)

    public void itemStateChanged(ItemEvent e)


Be the first to comment - What do you think?  Posted by The Kernel - June 10, 2011 at 2:09 am

Categories: Uncategorized   Tags: