• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking the characters

 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wanted to validate the field so that it must not accept the characters like / - ? : <>{}. + - .
How can i do this with applet awt components.
Thanks for u'r help in advance
uday
 
Frank Ying
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has KeyEvent & KeyAdapter functions that let you detect key strokes, you can simply write your own key adapter class extends KeyAdapter, and override the keyPress method. In that function, you check KeyEvent.getKeyCode(). Hope I explained clearly.
Frank
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uday,
In case you didn't understand Frank's explanation here is some code.

Enjoy,
Manfred.
 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
Do u mean like this i need to follow,
import java.awt.event.*;
class KeyCheck extends KeyAdapter
{
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_SLASH)
System.out.println("Slash was pressed");
}
}

then to call
TextField text = new TextField(30);
KeyCheck ck = new KeyCheck();
text.addKeyListener (ck);
then ck.keyPressed();// to call
Hope i am correct!! if so pls correct me

cheers
uday
 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
Do u mean like this i need to follow,
import java.awt.event.*;
class KeyCheck extends KeyAdapter
{
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_SLASH)
System.out.println("Slash was pressed");
}
}

then to call
TextField text = new TextField(30);
KeyCheck ck = new KeyCheck();
text.addKeyListener (ck);
then ck.keyPressed();// to call
Hope i am correct!! if so pls correct me

cheers
uday
 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfrid and Frank!!
I could able to get the Trap the characters worked

Cheers
uday
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic