Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form validation for JTextfield in Java?

Is there an easy way to validate a group of JTextFields in Java. I am currently using sqlite manager, neatbeans IDE to create a simple form that collects information to add a job into the database. I realise there is too many JTextFields. How can I validate each JTextFields either through sqllite throwing an exception error which can be customised, or maybe in Neatbeans through properties dialogue box. Validation is to be presence check, i.e. has a value been entered. Custom validation message?

like image 702
Hoody Avatar asked Jan 05 '13 08:01

Hoody


2 Answers

Use an InputVerifier, as shown in How to Use the Focus Subsystem : Validating Input. There's a related example here, among others.

like image 80
trashgod Avatar answered Sep 28 '22 15:09

trashgod


+1 t trashgods answer.

You could also use:

  • DocumentFilter/DocumentListener and add it to JTextField.

See here for an example and this variation.

DocumentFilter allows you to validate text before showing changes i.e by calling super.XXX implementation of overridden method after validating text successfully) where as DocumentListener does not. It usually in most cases better to settle for DocumentFilter.

See here for more help:

  • How to Write a Document Listener
  • Implementing a Document Filter

or alternatively:

  • JFormattedTextField see here for an example.
like image 31
David Kroukamp Avatar answered Sep 28 '22 14:09

David Kroukamp