Announcement Announcement Module
No announcement yet.
Disallow the user to type letters in numeric JTextFields Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Disallow the user to type letters in numeric JTextFields


    I've bound a JTextField in my form to an Integer property in my Entity. It works fine but I would like to disallow the user to type chars other than numbers into the field, maybe making a beep or something when typed. At the moment, the form points the error message correctly ("invalid format") but allows the user to keep typing letters. Advice is welcome.



  • #2
    If the form has errors, meaning the field contains an invalid format, nothing will be commited. The user has to use the proper format in order to continue, IMO there nothing wrong with that.

    You do have to check whether the form has errors though.


    • #3
      there nothing wrong with that
      Sure. But it's just a matter of taste. I would like the user to be unable to insert letters in numeric fields.



      • #4
        One way to approach this would be to create a custom Binder.


        • #5
          I change AsYouTypeTextValueSetter and add ValidationDocument - this document doesn't
          accept letter in numeric field, check date field or deny any changes (for class)

          You can add another check :

          source is on

          code have dependencies with rest of my framework.I will set demo , soon



          • #6
            Snpe's demo

            I started some weeks ago writting my first Spring-RCP application and I think that your demo will help me a lot so I'm very interesting to see your code soon


            • #7
              Look at the API for JTextField. See

              Now look at the example of an UpperCaseField. Within the implementation of insertString if you fail to call the super.insertString() then no characters are inserted into the field.

              Copy this example and provide your own implemenation of insertString() that filters out non-numeric characters from the string before passing it up to the super class.