In my code I have some JTextFields and also a separate JTable with multiple columns and rows. I want to validate a particular cell in my table, and if it is invalid, don't allow the user to move the cursor to another field. My code works correctly if the user moves the cursor to a different cell in the table (different row or column), but if I move the cursor to a different field on the form, the cursor is moved. I have verified that my stopCellEditing function is called and returning false. I turn the border red when it is invalid. That is working correctly and as expected, just the cursor is moving. Here is my code.
The cell editor to use for the column affected is set using
// Set up the verifier to make sure the user has entered a valid value
statusTable.getColumn(statusTable.getColumnName(1)).
setCellEditor(new CellEditor(new SvidVerifier(), this));
And my extended DefaultCellEditor is
class CellEditor extends DefaultCellEditor {
InputVerifier verifier = null;
ModView view = null;
public CellEditor(InputVerifier verifier, ModView view) {
super(new JTextField());
this.verifier = verifier;
this.view = view;
}
@Override
public boolean stopCellEditing() {
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
getFocusOwner() == view.publicBrowseButton) {
super.cancelCellEditing();
return true;
}
boolean canStop = verifier.verify(editorComponent) &&
super.stopCellEditing();
return canStop;
}
}
and my verifier is
class IdVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
JTextField tf = (JTextField) input;
try {
if ((Integer.parseInt(tf.getText()) >= 1) &&
(Integer.parseInt(tf.getText()) <= 32)) {
tf.setBorder(new LineBorder(
UIManager.getColor("activeCaptionBorder")));
return true;
} else {
tf.setBorder(new LineBorder(Color.RED));
return false;
}
} catch (Exception ex) {
tf.setBorder(new LineBorder(Color.RED));
return false;
}
}
}
Thank you for your help.