• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to avoid user to enter a numeric or alfanumeric input to JTextField  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm trying to use a method which is checking the input type of user..

simply if user enters a numerical character into the JTextField, the method wont allow it..

I sorted out how the method should be but.....



In addition to this , how can i use this method with a JTextField..

for eg. if i put a JTextField to the parameter..



Thanks for your time

Good Coding
 
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at javax.swing.event.DocumentListener
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setting the textfield's document might be one way

something like this

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This won't answer your question, but I find your code difficult to read. This is primarily because you are using the Unicode values of characters such as "(char) 58". What character is this? I don't typically have a ASCII or Unicode chart handy. It would be much clearer if you use char constants such as '0' or '9'.

Layne
 
john ersan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void NumericControl(java.awt.event.KeyEvent e)
{
if (((e.getKeyChar() >= (char) 58) || (e.getKeyChar() <= (char) 47)) != true )
// if typed input char ascii equivalent is not between ranges of numerical values 0 to 9

//char 47 = / , char 58 = : , between these are 0 - 9

{
if(e.getKeyChar() !=(char)8) // if char is not equal to -> ◘
{
e. // allow or avoid input
}
}
}
[ January 10, 2006: Message edited by: john ersan ]
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(not replying about JTextField question)
Did you know that KeyEvent class defines static variables for keys ?
Using getKeyCode with those like VK_0, VK_1 and so on would make your code much more readable.

With getKeyChar using things like '0', '1' would make it as readable too...
 
john ersan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx for your time guys..

the answer was..

e.consume();

thx again

good coding
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!