I have a table with a text column. I would like to ensure that the
values in that column remain unique when edited. I have created the
following class
class NameTextField extends JFormattedTextField {
Set currentNames = null;
public void setCurrentNames(Set currentNames) {
this.currentNames = currentNames;
}
public boolean isEditValid() {
String val = getFormatter().toString();
System.out.println("isEditValid called with '"+val+"'");
if (val == null || val.length() == 0 ||
currentNames.contains(val)) {
return false;
}
else {
return super.isEditValid();
}
}
}
I then set the CellEditor for the column with
TableColumn nameColumn =
getColumnModel().getColumn(AttributeTableModel.NAME_COL);
text = new NameTextField();
nameColumn.setCellEditor(new DefaultCellEditor(text));
I expect isEditValid() to be called each time I press 'Enter' after
editing the field. I would also expect that if I returned false, the
field would retain focus and force the user to correct the problem.
What I get is the method is not getting called at all. What am I doing
wrong? Do I need to define a Formatter? FormatterFactory?
> I have a table with a text column. I would like to ensure that the
> values in that column remain unique when edited. I have created the
> following class
>
> class NameTextField extends JFormattedTextField {
[...]
> I then set the CellEditor for the column with
>
[quoted text clipped - 7 lines]
> What I get is the method is not getting called at all. What am I doing
> wrong? Do I need to define a Formatter? FormatterFactory?
DefaultCellEditor does nothing to validate any input before stopping
edits - in your case that's more or less okay, because it need not
expect that the given textField actually is a JFormattedTextField which
has a special method to do so - but it not even consults any
InputVerifiers set to the editing components.
What you need to do is to provide a custom CellEditor which calls
isEditValid in editor.stopCellEditing(). There's an example in Sun's
Swing tutorial (How to use Tables) which might be helpful.
Jeanette