Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • 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: 17278
6
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic