0

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Harshit Sharma
  • 313
  • 4
  • 19
  • For reference, here's a complete, working [example](http://stackoverflow.com/a/11113648/230513) of adding a `CellEditorListener` to a `JTree`. – trashgod Jul 06 '13 at 13:10
  • why do you want to listener to the editor (vs. listening to the model)? Anyway, you should post a SSCCE to demonstrate the problem – kleopatra Jul 07 '13 at 15:47

0 Answers0