Search code examples
javaswingvalidationjformattedtextfield

Java Swing - JFormattedTextField


I have JFormattedTextField s in my form. I want to validate the length and characters that are being inserted in the field.

e.g FirstName must not allow a length more that 20 characters and should not allow any special and numeric characters being inserted. Space can be considered.

Please help me how can I achieve this goal.


Solution

  • I want to validate the length and characters that are being inserted in the field.

    Use a MaskFormatter with your JFormattedTextField. With the MaskFormatter you can:

    1. limit the number of characters
    2. provide a String of valid characters

    Read the API for more information and examples.