• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I force uppercase in TextField?

 
Fred Henkel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm subclassing a TextField and trying to force all chars to be upppercase. My code looks like this:

class Abc extends TextField{
Abc(String inString){
setText(inString);
enableEvents(AWTEvent.KEY_EVENT_MASK);
}

protected void processKeyEvent (KeyEvent e)
{
char c = e.getKeyChar();
System.out.println( c );
if (Character.isLetter(c)) {
System.out.println( "its a letter" );
if (Character.isLowerCase(c) ) {
System.out.println( "its a lower" );
c = Character.toUpperCase(c);
System.out.println( c );
e.setKeyChar(c);
}
}
e.setKeyChar(c);
super.processKeyEvent (e);
} // end processKeyEvent

}//END CLASS
It catches the char but has no effect on what I'm seeing in the TextField (ie case does not change). It seems I'm overriding the wrong method..any suggestions?
thanks!
 
Christian Hauser
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try something like this:

I know I've seen something like this before, but I don't remember where.
HTH,
Hausi
 
Christian Hauser
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tested it and it worked with the following code. Try using my UpperCaseTextField insted of a normal JTextField and it works:

Good luck,
Hausi
 
Fred Henkel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hausi,
Thanks for your help!
Fred
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!