Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to implement key selectable Choice list ?  RSS feed

 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!