JTable - removing row
Hello.
I am using a JTable that needs to have rows added and removed.
To remove the last row of the table I use the line:
Def_model.removeRow( table.getModel().getRowCount() - 1 );
Def_model is a DefaultTableModel and table is a JTable
After I removed the line I could do select another cell (with the mouse) the folowing exception was trown:
Exception in thread"AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 21 >= 21
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:648)
at javax.swing.JTable.setValueAt(JTable.java:2676)
at javax.swing.JTable.editingStopped(JTable.java:4675)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:330)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:215)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5419)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:986)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
at java.awt.Component.processMouseEvent(Component.java:6035)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3983)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
I did not understood what was hapenning. Ther is no reference to the class I was creating in the exception. What was I doing wrong?
Well, i had a KeyListener added to the DefaultCellEditor's Component. In the KeyListener i had a System.out.println( "MyClass etc... that was sending output every time a key was pressd, even after the line i was editing was removed. I changed My code for:
editor.stopCellEditing();
Def_model.removeRow( table.getModel().getRowCount() - 1 );
editor is a DefaultCellEditor object.
With this code every thig works fine.
It seem to me that JTabledo not stop editing a cell even if the line it was in was REMOVED.
Is this the expected behavior? I did not find any reference on this in the documentation.
This situation is very hard to debug because the exception is not trown from programers class.
Is this a swing bug?

