class MachineTableModel extends AbstractTableModel{
/**
 * 
 */
private static final long serialVersionUID = 1L;
java.util.ArrayList<Machine> mList=new ArrayList<Machine>(6);
Object[]   column=   new   String[]{"机器型号","机身号码","购买途径","购买时间","安装人员","安装时间"}; 
public MachineTableModel(){
addTableModelListener(new MachineTableModelListener());
}
public int getRowCount(){
return   mList.size(); 
}

public int getColumnCount(){
return column.length;
}

public String getColumnName(int i){
return column[i].toString();
} public Object getValueAt(int row, int col) {
Machine m=mList.get(row);
    if(col== 0){
     return m.getIndex();
    }else if(col== 1){
     return m.getName();
    }else if(col== 2){
     return m.getBuy_from();
    }else if(col== 3){
     return m.getBuy_date();
    }else if(col== 4){
     return m.getInstall();
    }else{
     return m.getIn_date();
    }
}

public void setValueAt(Object object, int row, int col){
Machine m=mList.get(row);
    if(col== 0){
 m.setIndex(object.toString());
    }else if(col== 1){
     m.setName(object.toString());
    }else if(col== 2){
     m.setBuy_from(object.toString());
    }else if(col== 3){
     m.setBuy_date(object.toString());
    }else if(col== 4){
     m.setInstall(object.toString());
    }else{
     m.setIn_date(object.toString());
    }
fireTableDataChanged();
}

public boolean isCellEditable(int i, int j){
return true;
}

class MachineTableModelListener implements TableModelListener{
public void tableChanged(TableModelEvent e) {
for(Machine m: mList){ 
for(int i= 0; i< column.length; i++){ 
if(i== 0){
System.out.print(m.getIndex()+"\t");
}else if(i== 1){
System.out.print(m.getName()+"\t");
}else if(i== 2){
System.out.print(m.getBuy_from()+"\t");
}else if(i== 3){
System.out.print(m.getBuy_date()+"\t");
}else if(i== 4){
System.out.print(m.getInstall()+"\t");
}else if(i== 5){
System.out.print(m.getIn_date()+"\t");
}else{

}
}
}
}
}
}
这个表格模型应该怎么样改写才可以?

解决方案 »

  1.   

    现在就是没有显示空白行,用户操作也不能输入
    欲达到的效果是
    输入一行完整数据后,就会生成Machine对象
    Machine类源码如下: 
    public   class   Machine   { 
    private   String   index; 
    private   String   name,   sn; 
    private   String   buy_from,   buy_date; 
    private   String   install,   in_date; 
    public   Machine(){ } public   Machine(String   name){ 
    this.name=   name; 
    } public   Machine(String   name,   String   sn){ 
    this.name=   name; 
    this.sn=   sn; 

    ....