• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox select onself the item  RSS feed

 
Grégoire Berclaz
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
When I type "j" in my editable JComboBox I like that the first item begining with "j" will be selected and if I type "ja" the first item beginig with "ja" will be selected in the same JComboBox...
Is it possible?
Greg
P.S. Tks for the answers!!!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the things that bothers me about the SWING API. What you are requesting is a very common function. And it is common in almost all Desktops; Windows, Linux, MAC.
And yet, there is no easy way to do this. I don't know why Sun couldn't have built this into the existing JComboBox. But alas, you have your work cut out for you.
I will be interested in finding an answer to this as well.
 
Chella Anu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
}
}
 
Grégoire Berclaz
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In french we say : Fabuleux!!!
Tks very much
Greg
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!