Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ENTER KEY with JComboBox  RSS feed

 
Kiran Baratam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to move the focus when user presses ENTER key.
I have 2 comboboxes and then some textfields.
I had implemented the KeyListener interface and overridedn keyPressed method.
when i press enterkey on a combobox its not movinf to next feild.Its working fine with TextField.from one text field to another focus is movinf on press of ENTER key.
Please suggest me if i am doing some thing wrong??
Here is the implementation.
public void keyPressed(KeyEvent evt)
{
int keyCode = evt.getKeyCode();
if (evt.getSource() == m_AccountCBox)//m_AccountCBox is a JComboBox
{
if (keyCode == KeyEvent.VK_ENTER)
{
m_SecurityCBox.requestFocus();
}
}
else if (evt.getSource() == m_SecurityCBox)//m_SecurityCBox is a JComboBox
{
if (keyCode == KeyEvent.VK_ENTER)
{
m_AmountTField.requestFocus();
}
}
else if (evt.getSource() == m_AmountTField)
{
if (keyCode == KeyEvent.VK_ENTER)
{
m_PriceTField.requestFocus();
}
}
else if (evt.getSource() == m_PriceTField)
{
if (keyCode == KeyEvent.VK_ENTER)
{
m_UnitsTField.requestFocus();
}
}
else if (evt.getSource() == m_UnitsTField)
{
if (keyCode == KeyEvent.VK_ENTER)
{
m_AmountTField.requestFocus();
}
}
}
 
Abhinav Anand
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I noticed the above behavior of JComboBox. I tried the same approach but it didn't work.
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_ENTER)
{
if(ke.getSource() == c1)
{
c2.requestFocus();
}
else if(ke.getSource() == c2)
{
textPane.requestFocus();
}
else if(ke.getSource() == tf1)
{
c1.requestFocus();
}
}
}
Why I don't know.. It's working for me. I have tesetd it with BorderLayout also..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!