• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data validation

 
Sam Stackly
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys
What is the best way for data validation of numeric input ?
1- in keypressed event
2- after posting data, in controller
Thanks
Sam
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to validate data in a JTextField is to us a PlainDocument, this can be used to prevent certain characters from being entered and also the length. Look at the API at the PlainDocument class, and extend it and you can create a NumericDocument, then extend the JTextField and create a NumericTextField.
Or check isDigit when you do #2. I think #2 is better because it won't fire as much as #1 will.

Mark
 
Sam Stackly
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark,
It's great to have you around here,

Sam
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!