有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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);

那它就可以正常工作了。也许有人能给我点启示,因为我不知道怎么了。谢谢<;三,

这里是控制台告诉我的顺便说一句:

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) 个答案

  1. # 1 楼答案

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

    从您正在使用的构造函数的源:

     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));
    }