gpt4 book ai didi

java - DefaultTableModel setValueAt 抛出 ArrayIndexOutOfboundsException

转载 作者:行者123 更新时间:2023-12-02 07:19:44 24 4
gpt4 key购买 nike

我的要求是在网格中显示 StockQuote 符号(流数据)。为此,我选择了 JTable

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Preethi {

protected void initUI() {
final Random random = new Random();

final DefaultTableModel model = new DefaultTableModel(new String[] { "Id", "Name", "Desg", "Sal" }, 0);
final JTable table = new JTable(model);
table.setFillsViewportHeight(true);
JFrame frame = new JFrame(Preethi.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

Random ran = new Random(12);
ran.nextFloat();

String str = new String("sss");

String[] socrates = {str, str, str };
model.addRow(socrates);

System.out.println(model.getRowCount());
System.out.println(model.getColumnCount());

model.setValueAt(new Object[]{"ewqeq","rtgre","gfdgd","mbmnb"}, 1, 4);
}

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new Preethi().initUI();
}
});
}
}

最佳答案

您无法设置不存在的行的值。

DefaultTableModeladdRow

正如 Jon 提到的,您不应该对数据对象使用静态方法和字段

关于java - DefaultTableModel setValueAt 抛出 ArrayIndexOutOfboundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14413393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com