This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes key event on JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "key event on JComboBox" Watch "key event on JComboBox" New topic
Author

key event on JComboBox

venkat paruchuri
Greenhorn

Joined: Aug 15, 2001
Posts: 28
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

Joined: Sep 10, 2001
Posts: 72
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

Joined: Aug 15, 2001
Posts: 28
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: key event on JComboBox
 
Similar Threads
trouble writing file
JComboBox
One more JComboBox in JTable problem...
Implementation of Autocomplete JComboBox in an applet
JComboBox drop-down event