如何修复由JTable事件生成的StackOverflowError? [英] How to fix a StackOverflowError generated by a JTable event?

查看:77
本文介绍了如何修复由JTable事件生成的StackOverflowError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试用同一行的两个其他单元格的乘积来填充一个单元格时,出现StackOverflowError. 这是代码:

I get a StackOverflowError when I am trying to fill a cell with the multiplication of two other cells of the same row. Here is the code :

tableModel.addTableModelListener(new TableModelListener(){
            public void tableChanged(TableModelEvent e)
            {
                DefaultTableModel model = (DefaultTableModel)e.getSource();
                //Object data = model.getValueAt(e.getFirstRow(), e.getColumn());
                if (e.getColumn() == 0)
                {
                    Object data = model.getValueAt(e.getFirstRow(), e.getColumn());
                    String stockSymbol = (String)data;
                    XMLService2 myService = new XMLService2(stockSymbol);
                    String stockName = XMLService2.getStockName();
                    model.setValueAt(stockName, e.getFirstRow(), e.getColumn() + 1);
                }
                if (model.getValueAt(e.getFirstRow(), 2) != null && model.getValueAt(e.getFirstRow(), 3) != null)
                {
                    Double myDouble =(Integer)model.getValueAt(e.getFirstRow(), 2)*(Double)model.getValueAt(e.getFirstRow(), 3);
                    model.setValueAt(myDouble, e.getFirstRow(), 4);
                }
            }
        });

此代码的最后一行调用setValueAt函数,将产生StackOverflowError. 谢谢.

The last line of this code that calls the setValueAt function is producing the StackOverflowError. Thank you.

PS: 该表包括5列. 第三列的类型为Integer. 第4列的类型为Double. 得到第3个单元格和第4个单元格相乘结果的第5列的类型为Double.

PS : The table consists of 5 columns. The type of the 3rd column is Integer. The type of the 4th column is Double. The type of the 5th column which gets the result of the multiplication of 3rd and 4th cell is Double.

推荐答案

由于最后一个model.setValueAt行触发了另一个tableChanged事件,因此该程序陷入了一个无限的递归循环(至少直到堆栈溢出).尝试将第二个条件语句更改为

The program is falling into an endless recursive loop (at least until the stack overflows) because the last model.setValueAt line fires another tableChanged event. Try changing the second conditional statement to

if (e.getColumn() != 4 && model.getValueAt(e.getFirstRow(), 2) != null && model.getValueAt(e.getFirstRow(), 3) != null)

这应防止在更新最后一列时重新触发事件.

This should prevent the event from re-firing when the last column is updated.

这篇关于如何修复由JTable事件生成的StackOverflowError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆