当setvalue到特定列时,JTable Java Error Stack OverFlow [英] JTable Java Error Stack OverFlow when setvalue to a specific column

查看:134
本文介绍了当setvalue到特定列时,JTable Java Error Stack OverFlow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码如下。我创建了一个包含4列和3行的Jtable。
并添加表模型监听器,在表中更改监听器,当我在特定列中设置值时,Stack Overflow错误即将到来。

This is my Code below. I have created a Jtable with 4 column and 3 rows. and add table model listener, In the Table change Listener, When i set the value in a particular column Stack Overflow error is coming.

**error is Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at java.nio.Buffer.<init>(Buffer.java:189)
    at java.nio.CharBuffer.<init>(CharBuffer.java:276)
    at java.nio.HeapCharBuffer.<init>(HeapCharBuffer.java:70)
    at java.nio.CharBuffer.wrap(CharBuffer.java:369)
    at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:265)**

代码是:

包裹测试;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

public class TableModelListenerDemo {

    public static void main(String args[]) {

        final Object rowData[][] = {{"1", "one", "I",null}, {"2", "two", "II",null}, {"3", "three", "III",null}};
        final String columnNames[] = {"#", "English", "Roman", "Test"};

        final JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        table.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                try {
                    System.out.println(e);
                    int row = table.getSelectedRow();
                    Object QTY = table.getValueAt(row, 0);
                    Object UPrice = table.getValueAt(row, 1);
                    Object Three = table.getValueAt(row, 2);
                    table.setValueAt(Three, row, 3);
                }catch(Exception ex){

                }

            }
        });

        table.setValueAt("", 0, 0);
        JFrame frame = new JFrame("Resizing Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(scrollPane, BorderLayout.CENTER);

        frame.setSize(300, 150);
        frame.setVisible(true);

    }
}


推荐答案

JTable #setValueAt 将调用 TableModel #setValueAt ,这将触发 TableChanged event,这就是为什么它会导致你 StackOverflowException (它被无限循环捕获)

JTable#setValueAt will call TableModel#setValueAt, which will trigger a TableChanged event, which is why it's causing you a StackOverflowException (it's caught in an infinite loop)

更好的解决方案是覆盖 TableModel #setValueAt ,如果列 0 1 2 更新,也计算列 3 的值,但不要不要忘记发起 cellUpdated 事件

The better solution would be to override the TableModel#setValueAt and if the column 0, 1 or 2 is updated, also calculate the value for column 3, but don't forget to fire a cellUpdated event

这篇关于当setvalue到特定列时,JTable Java Error Stack OverFlow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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