JAVA - Validation in JTable
java, jtable, swing, validation
Solution
You should implement a `TableCellEditor` and perform your validation within the `stopCellEditing()` method. If validation fails this method should return `false`. From the Javadoc:
"Tells the editor to stop editing and accept any partially edited value as the value of the editor. The editor returns false if editing was not stopped; this is useful for editors that validate and can not accept invalid entries."
Take a look at the GenericEditor class defined within `JTable` for an example of this.
One other thing worth looking at: You could always construct a `DefaultCellEditor` with a `JFormattedTextField` as a parameter and add an `InputVerifier` to the text field to prevent entry of invalid data from being committed.
Problem
I am trying to validate a cell in the `JTable` as soon as the data is entered. I want to make sure that say the correct code is entered in the code column. Could anyone let me know how to do this please?