JTable cell not going away! SOLVED
-
SOLUTION: This works for me but I still do not know why it was behaving like that...
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
}Was called when deleting the row, forcing the cell that is being edited to stop editing. PROBLEM: I have the following CellEditor that is used to validate cells on a column:
public class CellEditor extends AbstractCellEditor implements TableCellEditor {
protected TextField source = new TextField(); private ArrayList validators = new ArrayList(); public CellEditor WithValidation(IValidator validator) { validators.add(validator); return this; } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { source.setText(String.valueOf(value)); return source; } public Object getCellEditorValue() { return source.getText(); } @Override public boolean stopCellEditing() { String s = ((String) this.getCellEditorValue()); if (s == null) { this.fireEditingCanceled(); return false; } for (IValidator v : this.validators) { if (!v.isValid(s)) { JOptionPane.showMessageDialog(null, v.getErrorMessage()); this.fireEditingCanceled(); return false; } } return super.stopCellEditing(); }
}
And I attach the code to my table like this:
this.jTable1.getColumnModel()
.getColumn(0)
.setCellEditor(new loyaltyapp.validation.CellEditor()
.WithValidation(new TextLengthValidator(1, 12)));When the second snippet of code is commented everything works as intended (but of course not including any validation)... The problem is that when I do attach the CellEditor to my table when I remove a row WHILE a cell is being edited the rest of the row gets deleted, leaving behind a rogue cell! Image of selecting a cell: http://www.mediafire.com/?8n999a4djacdj84[^] Image of deleting the row while selecting the validation cell: http://www.mediafire.com/?mozjjzc53ju12b2[<
-
SOLUTION: This works for me but I still do not know why it was behaving like that...
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
}Was called when deleting the row, forcing the cell that is being edited to stop editing. PROBLEM: I have the following CellEditor that is used to validate cells on a column:
public class CellEditor extends AbstractCellEditor implements TableCellEditor {
protected TextField source = new TextField(); private ArrayList validators = new ArrayList(); public CellEditor WithValidation(IValidator validator) { validators.add(validator); return this; } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { source.setText(String.valueOf(value)); return source; } public Object getCellEditorValue() { return source.getText(); } @Override public boolean stopCellEditing() { String s = ((String) this.getCellEditorValue()); if (s == null) { this.fireEditingCanceled(); return false; } for (IValidator v : this.validators) { if (!v.isValid(s)) { JOptionPane.showMessageDialog(null, v.getErrorMessage()); this.fireEditingCanceled(); return false; } } return super.stopCellEditing(); }
}
And I attach the code to my table like this:
this.jTable1.getColumnModel()
.getColumn(0)
.setCellEditor(new loyaltyapp.validation.CellEditor()
.WithValidation(new TextLengthValidator(1, 12)));When the second snippet of code is commented everything works as intended (but of course not including any validation)... The problem is that when I do attach the CellEditor to my table when I remove a row WHILE a cell is being edited the rest of the row gets deleted, leaving behind a rogue cell! Image of selecting a cell: http://www.mediafire.com/?8n999a4djacdj84[^] Image of deleting the row while selecting the validation cell: http://www.mediafire.com/?mozjjzc53ju12b2[<
The problem is that when I do attach the CellEditor to my table when I remove a row WHILE a cell is being edited the rest of the row gets deleted, leaving behind a rogue cell! Then decide - Editing OR deleting. Can't have both. Is the system deleting the row or how is it done?
regards Torsten When I'm not working