• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to implement key selectable Choice list ?  RSS feed

 
Lakshmi Geetha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using java.awt.Choice to display all the states in US, Canada and Mexico in that order. I need to make this list selectable using key board. ie, if i press A, it should first go to AL , then to AK etc. I have added the following code in the class which extends from Choice so that it i type A , it will go to AL. But subsequent key types for the same letter are not recognised. If anybody can provide code for this functionality, it will be great.
protected void processEvent(AWTEvent evt)
{
super.processEvent(evt);
if( evt instanceof KeyEvent)
{
char ch = ((KeyEvent)evt).getKeyChar();
Character c = new Character(ch);
if(!Character.isLetterOrDigit(ch))
{
return;
}
for(int i =0; i < getItemCount(); i++)
{
if( getItem(i).substring(0,1).equalsIgnoreCase(c.toString()))
{
select(i);
break;
}
}
}
}

Thanks in advance.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!