Try this code snippet for the required result:
class AMemComboAgent extends KeyAdapter
{
protected JComboBox m_comboBox;
protected JTextField m_editor;
public AMemComboAgent(JComboBox comboBox)
{
m_comboBox = comboBox;
m_editor = (JTextField) comboBox.getEditor().getEditorComponent();
m_editor.addKeyListener(this);
}
public void keyReleased(KeyEvent e)
{
char ch = e.getKeyChar();
if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch))
return;
int pos = m_editor.getCaretPosition();
String str = m_editor.getText();
if (str.length() == 0)
return;
int m=m_comboBox.getItemCount();
for (int k = 0; k < m; k++)
{
String item = m_comboBox.getItemAt(k).toString();
if (item.startsWith(str))
{
m_editor.setText(item);
m_editor.setCaretPosition(item.length());
m_editor.moveCaretPosition(pos);
break;
}
}
}
}