我要在JTable中更改值之后保存,现在重写了一下setValueAt方法
public class TableData extends AbstractTableModel 
{
Vector myTable;  
int colCount;
String [] headers = {"Filename","Size - Kb"};
int totalFileSize;
PostletLabels pLabels; public TableData(String file, String size){ myTable = new Vector();
colCount = 2;
totalFileSize =0;
headers[0] = file;
headers[1] = size;
} public String getColumnName(int i){
if(i==1 && totalFileSize !=0)
{
double totalFileSizeMB = totalFileSize/ 10485.76;
totalFileSizeMB = (double)Math.round(totalFileSizeMB)/100;
return headers[i]+" ("+totalFileSizeMB+"Mb)";  
}
else
return headers[i];
} public int getColumnCount(){
return colCount; 
}
 
public int getRowCount()
{
return myTable.size();}

public int getTotalFileSize()
{
return totalFileSize;}

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

public void setValueAt(Object value, int row, int col)
{
((Vector)myTable.elementAt(row)).setElementAt(value, col);
fireTableCellUpdated(row, col);
//super.setValueAt(value, row, col);
}

public boolean isCellEditable(int row, int col)
{
if(col == 0)
{
return true;
}
else
{return false;}
}
}
结果是双击单元格编辑后无法推出编辑状态,但是只要注释掉((Vector)myTable.elementAt(row)).setElementAt(value, col);就可以正常推出编辑状态。也试过
public void setValueAt(Object value, int row, int col)
{
//((Vector)myTable.elementAt(row)).setElementAt(value, col);
//fireTableCellUpdated(row, col);
super.setValueAt(value, row, col);
}
可正常推出编辑状态但是无法保存编辑的数据,求解?
网上看了看有个方法
tabledata = new TableData(pLabels.getLabel(0),pLabels.getLabel(1)+" -KB ");
table = new JTable(tabledata);
table.setColumnSelectionAllowed(false);
if (table.isEditing())
{table.getCellEditor(table.getEditingRow(), table.getEditingColumn()).stopCellEditing();}

试了试没有效果,不知道写在哪里起作用??

解决方案 »

  1.   

    你是不是设置了enable(false)?下面是转一位老兄的    /*  告知编辑器停止编辑并接受任何已部分编辑的值作为编辑器的值 <br>
         * @param cellEditor <br>
         * @return 如果 cellEditor对象为null 或 编辑已停止,则返回 true;否则返回 false <br>
         * 2009/02/20 <br>
         * 15:49:22 <br>
         * @author csj <br>
         */
        private boolean stopCellEditing(CellEditor cellEditor){
        if(cellEditor!=null){
            return cellEditor.stopCellEditing();
        }
        return true;
        }