defaulttablemodel.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
System.out.println("e.getLastRow():"+e.getLastRow());
String s=table.getValueAt(e.getLastRow(),e.getColumn()).toString();
new_val=s;
//System.out.println("结果2:"+new_val);
if(old_val!=null && !old_val.trim().equals("")){
if(!new_val.equals(old_val)){
int n = JOptionPane.showConfirmDialog(null, " 确认修改?", "标题",JOptionPane.YES_NO_OPTION);//返回的是按钮的index i=0或者1
if(n==0){
/**
* 修改 保存到xml
*
*/
}else{
/**
* 计算上序号
*/
table.setValueAt(old_val, e.getLastRow(),e.getColumn());
}
}
}else{
if((table.getSelectedRow()+1)==table.getRowCount()){
String[] myrow = {"", "", "", "","",""};
// defaulttablemodel.addRow(myrow);
//这里添加不了
}
}
}
});
http://www.360doc.com/content/15/0805/15/1180274_489675626.shtml
用DefaultTableModel方便很多。
public class RoomTable extends JTable{
dtm = new DefaultTableModel();
//使用向量设定表头,为了使表格适用于数据的获取和显示
ColumnName = new Vector();
ColumnName.add("房号");
ColumnName.add("楼层");
ColumnName.add("配置");
ColumnName.add("是否入住");
ColumnName.add("价格");
dataVector = new Vector<>();
dtm.setDataVector(dataVector, ColumnName);//通过表格模板设置表格数据,这里面的dataVector是二维向量,也就是表格中的数据
dtm.setRowCount(20);//设置初始行数
setModel(dtm);//加载表格模板
display();
//添加一条新行/记录
public void addRecord(){
Vector room = (Vector) dtm.getDataVector().lastElement();//获取最后一行对象
String str = room.get(0).toString().replaceAll("\s*", "");//得到行ID号
int last = Integer.parseInt(str);
room = new Vector();
room.add(last+1);//新增一行,IDh号+1
room.add(null);
room.add(null);
room.add(null);
room.add(null);//此处自行优化
dtm.getDataVector().add(room);//将新行元素加入到表格数据末尾
dtm.setDataVector(dtm.getDataVector(), ColumnName);//重新加载表格数据
}