JFormatterTextField.isEditValid() not being called?

B

Boris Pelakh

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?
 
K

Kleopatra

Boris said:
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

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top