Reputation: 802
I have a JTable with 3 columns. One on the columns must have an integer value and I must validate the input before the cell loses its focus.
I've used cell editor and overrided the stopCellEditing() function. I've wrote the validation in stopCellEditing and it does keep the focus on the cell but but my problem is this:
this is my editor class:
public class MyEditor extends DefaultCellEditor implements TableCellEditor {
public MyEditor() {
super(new JTextField());
}
@Override
public boolean stopCellEditing() {
Object obj = delegate.getCellEditorValue();
if (obj is not an integer) {
return false;
}
return true;
}
and this is how I'm using in in my Frame:
studentsTable.getColumnModel().getColumn(2).setCellEditor(new MyEditor());
Plz help me :)
Upvotes: 1
Views: 2132
Reputation: 324157
One on the columns must have an integer value
No need to write a custom editor for this.
All you need to do is override the getColumnClass()
method of JTable or your TableModel and the table will use the supplied Integer editor.
Regarding the code you posted:
It won't compile since your if condition is not valid. We want real code so we can spot possible logic errors. The code should also be posted in the form of an SSCCE.
There is no need to reference the delegate variable. Just invoke the getCellEditor() method directly.
Don't know if it makes a difference bu when I override stopCellEditing(), instead of returning true I use:
return super.stopCellEditing();
Upvotes: 2