当我创建这个Swing JTable时,为什么vector的elementCount没有增加?

时间:2014-07-09 18:59:43

标签: java swing jtable

我继承了一个使用Swing的Java应用程序,我没有经验,所以请耐心等待。

当我尝试打开应用程序的某个功能时,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 0
  at java.util.Vector.elementAt(Vector.java:470)
  at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
  at AssetTable$ComponentTable.<init>(AssetTable.java:69)

表模型的类如下所示:

   static class ComponentTable extends JTable
   {
     JComboBox ocTypesCBox;
     AssetTable.ComponentTable.ComponentTableModel ctmodel;

     public ComponentTable(AssetTable.ComponentTable.ComponentTableModel ctm)
     {
       super();
       this.ocTypesCBox = new JComboBox();
       this.ocTypesCBox.addItem("1");
       this.ocTypesCBox.addItem("2");
       this.ocTypesCBox.addItem("3");
       this.ocTypesCBox.addItem("4");
       getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(this.ocTypesCBox));
       this.ctmodel = ctm;
     }

     public void setData(OnlineComponent oc) {
       Object[][] ocData = { { oc.getCode(), oc.getIntegratorId(), oc.getPP(), oc.getType() } };
       setData(ocData);
     }

     public void setData(Object[][] data)
     {
       this.ctmodel.setData(data);
     }

     public Object[][] getData() {
       return this.ctmodel.getData();
     }

     static class ComponentTableModel extends AbstractTableModel {
       String[] ocColumns = { "Component_Code", "Integrator_ID", "Popup_Properties", "Type" };
       Object[][] ocData = { { "", "", "", "" } };

       public void setData(Object[][] ocData) {
         this.ocData = ocData;
         fireTableDataChanged();
       }
       public Object[][] getData() {
         return this.ocData;
       }
       public int getColumnCount() { return this.ocColumns.length; }


       public int getRowCount()
       {
         return this.ocData.length;
       }

       public boolean isCellEditable(int row, int col)
       {
         if (AssetTable.AssetTableModel.course) return true;
         return false;
       }

       public Object getValueAt(int row, int col)
       {
         return this.ocData[row][col];
       }

       public void setValueAt(Object value, int row, int col)
       {
         this.ocData[row][col] = (value == null ? "" : (String)value);
         fireTableCellUpdated(row, col);
       }

       public String getColumnName(int col) {
         return this.ocColumns[col];
       }
     }
   }

getColumn(3)电话发生错误。在我看来,这些addItem方法实际上并未增加此表的elementCount的预期大小。为什么会这样?

我怀疑这个代码还远远不够,但我不确定什么是相关的,什么是TMI。如果你能想到一些有用的东西,我会尝试追踪它并将其作为编辑发布。

1 个答案:

答案 0 :(得分:0)

你必须使用:

super(ctm);

这通过构造函数

将tablemodel设置为JTable