Question

I have created a JTable using following code. Then i add two new columns. Then after making the row count zero, when i try to add a new row to the table i get array out of bound exception. Please help.

//creating table structure
jTable2 = new javax.swing.JTable();
jTable2.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
        },
        new String [] {
            "Distance (km)", "Current (A)", "Resistance (Ω)"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, true
        };
        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });

//adding two new columns
DefaultTableModel dtm  = (DefaultTableModel) jTable2.getModel();
dtm.addColumn("Voltage (V)");
dtm.addColumn("Power (W)");    

dtm.setRowCount(0);

//adding new row
dtm.addRow(new Object[]{1,5,2,4,6});

//i get this errors
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at    power_distribution.View.DC_Concentrated_One$1.getColumnClass(DC_Concentrated_One.java:93)
at javax.swing.JTable.getColumnClass(JTable.java:2698)
at javax.swing.JTable.getCellRenderer(JTable.java:5687)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2113)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Was it helpful?

Solution

Your problem is when you add a new column, you need to extend your types variable, because that holds values only for 'default' columns, and when getColumnClass() called for new columns that throws ArrayIndexOutOfBoundsException.

EDIT:

Example of extending your model, just override addColumn() method like next:

 @Override
 public void addColumn(Object columnName) {
       Class[] temp = new Class[types.length +1];
       System.arraycopy(types, 0, temp, 0, types.length);
       temp[types.length] = Double.class;
       types = temp;
       super.addColumn(columnName);
 }

Also examine @mKorbel comment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top