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

  • Disallow the user to type letters in numeric JTextFields

    Hi,

    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.

    Thanks,

    Waldemar

  • #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.

    Comment


    • #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.

      Waldemar

      Comment


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

        Comment


        • #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 http://www.snpe.co.yu/RevertAsYouTyp...lueSetter.java

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

          regards

          Comment


          • #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

            Comment


            • #7
              Look at the API for JTextField. See http://java.sun.com/j2se/1.4.2/docs/...TextField.html.

              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.

              Comment

              Working...
              X