• Post Reply Bookmark Topic Watch Topic
  • New Topic

Limiting Input to a JTextField to numbers

 
srinivasan ganesan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to limit the input to a JTextField to numbers alone. So, if anything other than a number is typed, it should not be shown.
Any ideas?
Thanks,
Srini
 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an idea to your problem..
put up a key pressed or key typed handler for the textfield.
then check every character that is typed.
approximation:
keypressed (KeyEvent e){
if (e.getKeyCode() > <intvalue> && e.getKeyCode() <intvalue> {
/*ok*/
}
else
return;
here you have to check the ascii values for the numbers (the characters that is allowed)
if it is not among the characters you want typed, then return.
i think it can be done this way, but i have never tried myself..
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the java tutorial. There is an example for you there.
 
srinivasan ganesan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your responses. Thanks for your help.
Srini
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!