tags:

views:

127

answers:

3

I'm not sure why this is recursing.

jTable1.getModel().addTableModelListener(new TableModelListener() {

 public void tableChanged(TableModelEvent evt) {
  int sum = 0;
  int i=0;
  for (i =0 ; i<2; i++){
   sum = sum + Integer.parseInt(jTable1.getValueAt(0, i).toString());
  }
  jTable1.setValueAt(sum, 0, 2);
 }

}); 

The exception is: (it keeps repeating)

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
        at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
        at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2553)
        at javax.swing.JTable.getValueAt(JTable.java:2695)
        at testprogram.guitest.TestTableModel$1.tableChanged(TestTableModel.java:63)
        at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
        at javax.swing.table.AbstractTableModel.fireTableCellUpdated(AbstractTableModel.java:259)
        at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:650)
        at javax.swing.JTable.setValueAt(JTable.java:2719)

Any help appreciated.

+1  A: 

JTable.setValueAt causes a tablechanged event to fire, so you're calling the event handler repeatedly from within the event handler. Set the value in the model, not in the table.

Paul Tomblin
+1  A: 

You're updating a value in an event handler for updates. This will naturally trigger the event handler to be called again. Which will trigger the event handler to be called again, etc.

You'd need to perhaps remove the listener before making the update.

Yuliy