I'm trying to dynamically add rows to the JTable, but having hard time with the ArrayIndexOutOfBounds exception. I'm confused because there is no specific explanation why the problem exists and where the problem occures. See below for the code.
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");
The problem only occures after pressing 2 or more times on the button. Can anyone help me how to dynamically empty table and add new data in it?
Edit: Here is the Error Log:
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)