Search code examples
javaswingjtextfield

Is there an option a JTextField can check the input on a specific format?


I want to use a JTextField to enter dates in the YYYY-MM-DD format. Is there a possibility to validate the entered format?


Solution

  • You are asking about Input Validation. Do this yourself with an event listener that is triggered

    • either whenever the field content changes,
    • or when the field loses focus
    • or when the dialog's ok button is pressed

    Which mechanism you use depends on how quickly you want to give feedback and how costly a validation is.

    As an alternative you can refer to the Swing Frameworks Input Validation features.