2014-04-02 1 views
3

다음 코드를 사용하여 JTable을 만들었습니다. 그런 다음 두 개의 새로운 열을 추가합니다. 그런 다음 행 수를 0으로 만든 후 테이블에 새 행을 추가하려고하면 배열 예외가 발생합니다. 도와주세요.jTable에 새 열을 추가 한 후 새 행을 삽입하면 배열에서 경계 외부 예외가 발생합니다.

//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) 
+0

표 셀 유형을 java.lang.Double로 변경하지 않고 Object로 유지하면 정상적으로 작동합니다. –

답변

4

귀하의 문제는 당신이 새 열을 추가 할 때, 당신이 단지 '기본'컬럼의 값을 보유하고 있기 때문에, 당신의 types 변수를 확장해야하고, getColumnClass()ArrayIndexOutOfBoundsException을 던져 새로운 열을 요구하는 경우.

편집 :

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

는 또한 @mKorbel 코멘트를 검사 : 모델을 확장

예, 바로 옆에 같은 addColumn() 메소드를 오버라이드 (override).

+0

저는 자바를 처음 사용합니다. u 좀 더 설명해 주시겠습니까? 유형 변수 란 무엇입니까? –

+1

@Rajind Ruparathna 더티 해킹은'return Double.class; '로'return types [columnIndex];를 대체 할 수 있습니다. return getValueAt (0, column) .getClass();' – mKorbel

+0

'types' 변수 재정의 된 'DefaultTableModel'에서 열 유형을 보유합니다. – alex2410