• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox select onself the item

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
In french we say : Fabuleux!!!
Tks very much
Greg
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic