I have a JFrame
named 'NewJFrame' containing a JTable
named 'inputTable'. I have an editor named 'myeditor1' which is an instance of the class MyEditor defined as follows :
private static class MyEditor extends DefaultCellEditor {
public MyEditor(){
super(new JTextField());
}
}
I have defined a class 'MyCellEditorListener' as follows :
public class MyCellEditorListener implements CellEditorListener{
public MyCellEditorListener(){
}
@Override
public void editingStopped(ChangeEvent e) {
System.out.println("Editing has been stopped");
}
@Override
public void editingCanceled(ChangeEvent e) {
System.out.println("Editing has been canceled");
}
}
inputTable's model is as follows :
inputTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Float.class, java.lang.Object.class, java.lang.Object.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
And the constructor of NewJFrame is :
public NewJFrame() {
initComponents();
MyEditor myeditor1 = new MyEditor();
inputTable.setDefaultEditor(Integer.class, myeditor1);
inputTable.setDefaultEditor(Float.class, myeditor1);
myeditor1.addCellEditorListener(new MyCellEditorListener());
}
My first problem is that :
Whenever I stop editing any cell in the second column (which is of type Float
), I get an exception saying that Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
but this doesn't happen when I stop editing a cell of the first column (which is of type Integer
). The output 'Editing has been stopped' is displayed in the second case but not in the first case !! Why is this happening and what should I do ?
My second problem is that :
Whenever I cancel editing a cell by pressing the Esc
key, the expected output of 'Editing has been canceled' is not displayed. Why ? Shouldn't a ChangeEvent
be triggered whenever I press the Esc
key (while editing a cell of inputTable) ?
Thanks in advance
P.S : I am using Netbeans IDE 7.2 RC1.