问题描述
我有一个JButton
,一旦按下它,就会在JTable
中添加一行.我试图通过实现以下代码来实现这一点.
I have a JButton
which, once it is pressed, adds a row into a JTable
. I have tried to make this by implementing the following code.
columNames = new Vector<>();
columNames.addElement("Name");
columNames.addElement("CC");
columNames.addElement("Age");
columNames.addElement("PhoneNumber");
columNames.addElement("Date");
columNames.addElement("Amount$");
Object[] dataList = {"name", "cc", "age", "phone", "date", "amount"};
data = new DefaultTableModel(columNames, 0);
data.addRow(dataList);
table = new JTable(data);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setBounds(22, 78, 764, 177);
scrollTable.setViewportView(table);
//ActionListener method!.
if(e.getActionCommand().equals("Add client"))
{
Object[] dataList = {"name", "cc", "age", "phone", "date", "amount"};
data.addRow(dataList);
DefaultTableModel defaut = (DefaultTableModel) table.getModel();
defaut.addRow(dataList);
}
它抛出Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
:-1
我该如何解决?
推荐答案
如下面的完整示例所示,您的片段似乎正常工作.该示例可以帮助您在完整代码中隔离问题.另外,
As shown in the complete example below, your fragments appear to work correctly. The example may help you isolated the problem in your full code. In addition,
-
代替
setBounds()
,覆盖getPreferredScrollableViewportSize()
以返回getRowHeight()
的倍数,如建议的此处和pack()
封闭窗口.
Instead of
setBounds()
, overridegetPreferredScrollableViewportSize()
to return a multiple ofgetRowHeight()
, as suggested here, andpack()
the enclosing window.
另请参见 是什么原因导致java.lang.ArrayIndexOutOfBoundsException以及如何防止它发生?
根据建议此处,重复更新失败TableModel
建议您可能引用了对象的意外实例.
As suggested here, repeated failures to update the TableModel
suggests that you may be referencing an unintended instance of an object.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* @see https://stackoverflow.com/a/38926460/230513
*/
public class Test {
private final Object[] dataList = {"name", "cc", "age", "phone", "date", "amount"};
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector columNames = new Vector<>();
columNames.addElement("Name");
columNames.addElement("CC");
columNames.addElement("Age");
columNames.addElement("Phone");
columNames.addElement("Date");
columNames.addElement("Amount$");
DefaultTableModel data = new DefaultTableModel(columNames, 0);
data.addRow(dataList);
JTable table = new JTable(data);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setViewportView(table);
f.add(new JScrollPane(table));
f.add(new JButton(new AbstractAction("Add") {
@Override
public void actionPerformed(ActionEvent e) {
data.addRow(dataList);
}
}), BorderLayout.PAGE_END);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
这篇关于将行添加到JTable时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!