JTable导致我的应用程序崩溃

时间:2016-01-12 14:46:14

标签: java swing crash jtable

我正在为学校项目创建一个相当简单的应用程序。它是联系经理。我想在表格中显示所有联系人,之前我从未使用过JTables,这让我感到困惑。我做了很多研究,我发现只有一个与我有同样问题的帖子,但它没有答案或任何有用的东西。

JVM crash when using JTable

这是我创建表格的代码。只是一个抬头,我不打算创造任何复杂的东西。只是一个表格,我可以插入和删除数据。这就是全部。

String[] columnLabels = {"Name", "Number", "Email", "Address"};
Object[][] data = {{"test 1", "test 2"}, {"test 3", "test 4"}};

JTable table = new JTable(data, columnLabels);

如果我只是插入

JTable table = new JTable(10,4);

然后它会正常工作。也许有人可以说清楚,因为我不知道什么是错的。谢谢< 3

以下是控制台告诉我的信息:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at javax.swing.JTable$1.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:4)

你的模型数组包含2行2列,而你的列名数组包含4个元素,因此有4列。这就是问题所在。提供一个数据数组,每行4个元素(可能为null),它应该可以工作。

从您使用的构造函数的来源:

 public int getRowCount() { return rowData.length; }
 public int getColumnCount() { return columnNames.length; }
 public Object getValueAt(int row, int col) { return rowData[row][col]; }

正如您所看到的,渲染器会查看columnNames的长度(列数为4),并对其进行迭代(因此为0 - 3)。最终它调用getValueAt(0, 2),然后调用rowData[0][2]和 - boom - col索引超出范围。

相比之下JTable table = new JTable(10,4);只创建一个包含10行和4列空模型的表:

public JTable(int numRows, int numColumns) {
    this(new DefaultTableModel(numRows, numColumns));
}