Results 1 to 7 of 7

Thread: Disallow the user to type letters in numeric JTextFields

  1. #1
    Join Date
    Jun 2005
    Posts
    16

    Default 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. #2
    Join Date
    Mar 2005
    Location
    Edegem, Belgium
    Posts
    6

    Default

    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.
    Koen.

  3. #3
    Join Date
    Jun 2005
    Posts
    16

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Posts
    229

    Default

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

  5. #5
    Join Date
    Aug 2004
    Posts
    203

    Default

    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

  6. #6
    Join Date
    Jul 2005
    Posts
    4

    Default 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. #7
    Join Date
    Jun 2005
    Location
    Yorkshire, England
    Posts
    9

    Default

    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.

Similar Threads

  1. Problem with HibernateInterceptor
    By prane in forum Data
    Replies: 5
    Last Post: Oct 16th, 2007, 08:01 AM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •