Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

key event on JComboBox

 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it possible to handle key events on JComboBox? I need to fire an event on JCOmboBox by hitting 'Enter Key' on it. Please help me ASAP. Immediate help is appreciated..
thanks
venkat
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first, please make yourComboBox editable.
Try this:
class CBAgent extends KeyAdapter
{
protected JComboBox m_comboBox;
protected JTextField m_editor;
public CBAgent(JComboBox comboBox)
{
m_comboBox = comboBox;
m_editor = (JTextField)comboBox.getEditor().getEditorComponent();
m_editor.addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
System.out.println("Howdy!");
}
}
}
Add keyEvent to your CombiBox
CBAgent agent = new CBAgent(yourCB);
I hope that can solve your problem.
Good lucks!
 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renee Zhang,
Thank you very much for your great help. I need to implement the same functionality in two places. I could manage with a single Adapter class effectively. I could overcome the one week long problem with your help.
thanks again,
venkat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic