简体   繁体   English

StackOverflowError由TableModelListener引起

[英]StackOverflowError being caused by a 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)

Any help appreciated. 任何帮助赞赏。

From the event handler (tableChanged method) wrap your code that makes use of setValue method with code to remove and add the listener, like this 从事件处理程序(tableChanged方法)包装你的代码,利用setValue方法和代码来删除和添加监听器,就像这样

public void tableChanged(TableModelEvent e) {
model.removeTableModelListener(this);
// YOUR CODE WITH setValueAt calls begins here
if (ppt == null || cantidad > ppt.getStock()) {

model.setValueAt(ppt != null ? ppt.getStock() : 0, e.getFirstRow(), 3);
}

model.setValueAt(precioUnitario * cantidad, e.getFirstRow(), 4);

// YOUR CODE with setValueAt calls ends here
model.addTableModelListener(this);
}

This will disable temporarily the listener while you work on validations. 这将在您进行验证时暂时禁用侦听器。

JTable.setValueAt causes a tablechanged event to fire, so you're calling the event handler repeatedly from within the event handler. JTable.setValueAt导致tablechanged事件触发,因此您将在事件处理程序中重复调用事件处理程序。 Set the value in the model, not in the table. 在模型中设置值,而不是在表中。

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. 在进行更新之前,您可能需要删除侦听器。

I was facing the same problem earlier when using jlist . 我在使用jlistjlist了同样的问题。 My solution was to simply change the sequence of my lines of code. 我的解决方案是简单地改变我的代码行的顺序。

  1. Get all the jpanel s which have images. 获取所有具有图像的jpanel
  2. Add a listener just before adding object[] to jlist 在将object[]添加到jlist之前添加一个监听jlist

The issue I previously had (which was creating a stack-overflow error was adding the listener to jlist first, then getting all of the jpanel s with images. 我之前遇到的问题(创建stack-overflow error是首先将监听器添加到jlist然后使用图像获取所有jpanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM