我正在尝试向 JTable 动态添加行,但遇到 ArrayIndexOutOfBounds 异常时遇到了困难。我很困惑,因为没有具体解释为什么问题存在以及问题发生在哪里。请参阅下面的代码。
ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
Object[][] data;
Random rand = new Random();
******************** WHEN PRESSING A BUTTON *****************************
matrix.add(new ArrayList<String>());
((ArrayList<String>)matrix.get(0)).add("row 0 col " + rand.nextInt(100) );
((ArrayList<String>)matrix.get(0)).add("row 0 col 1");
((ArrayList<String>)matrix.get(0)).add("row 0 col 2");
matrix.add(new ArrayList<String>());
((ArrayList<String>)matrix.get(1)).add("row 1 col 0");
((ArrayList<String>)matrix.get(1)).add("row 1 col 1");
((ArrayList<String>)matrix.get(1)).add("row 1 col 2");
data = new Object[matrix.size()][];
for(int i = 0; i < matrix.size(); i++){
data[i] = matrix.get(i).toArray();
}
table = new JTable(data, new String[]{"A", "B", "C"});
table.setFillsViewportHeight(true);
table.setForeground(Color.WHITE);
table.setBackground(Color.BLACK);
table.setBounds(0, 0, 423, 91);
pnlTable.add(table);
pnlTable.setLayout(null);
table.updateUI();
System.out.println("done");
只有在按钮上按 2 次或更多次后才会出现此问题。谁能帮助我如何动态清空表并在其中添加新数据?
编辑:这是错误日志:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
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.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._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(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$700(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$000(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$1.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)