问题描述
packagetablemodel;importjavax.swing.table.AbstractTableModel;publicclassMyTableModelextendsAbstractTableModel{privatestaticfinallongserialVersionUID=1L;privateString[]columnName;privateObject[][]data;privateintii;publicMyTableModel(Object[][]data,String[]columnName,intii){this.data=data;this.columnName=columnName;this.ii=ii;}publicMyTableModel(Object[][]data,String[]columnName){this.data=data;this.columnName=columnName;}//publicClass<?>getColumnClass(intc){//returnthis.getValueAt(0,c).getClass();//}publicStringgetColumnName(intcol){returncolumnName[col];}publicbooleanisCellEditable(introw,intcol){if(col<1)returnfalse;elseif(col==ii)returnfalse;elsereturntrue;}publicvoidsetValueAt(Objectvalue,introw,intcol){data[row][col]=value;this.fireTableCellUpdated(row,col);}@OverridepublicintgetColumnCount(){returncolumnName.length;}@OverridepublicintgetRowCount(){returndata.length;}@OverridepublicObjectgetValueAt(introwIndex,intcolumnIndex){returndata[rowIndex][columnIndex];}}
解决方案
解决方案二:
Swing新增加的表格组件,生成一个MyTableModel类型的对象来表示数据,这个类是从AbstractTableModel类中继承来的,其中有几个重写了的方法。楼主可以看看API