本文介绍了StackOverflowError由TableModelListener引起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定这是为什么会再次出现。
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)
感谢任何帮助。
推荐答案
JTable.setValueAt导致一张桌子将事件更改为触发,因此您将在事件处理程序中重复调用事件处理程序。设置模型中的值,而不是表中的值。
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.
这篇关于StackOverflowError由TableModelListener引起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!