在上表中,表中有4个值由序列号表示(S.NO 1,2,3,4)
问题是我们从像S.NO 2这样的表中删除一行
然后使用removeRow方法删除一行但不调整S.No S.NO的代码就像我引入了一个名为tree的int变量,当我们按下add按钮时,它会在树中添加一个值
int tree=0;
当我们按下ADD按钮
tree++;
我也试试
.getRowCount()
获取它的行号并打印它然后再次当我们删除一行并添加另一行时它只打印该行号。那么如何调整S.No
添加按钮代码(用于在表格和数据库中添加数据)
class google{
int tree=0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //add Button method
model=(DefaultTableModel) mtt.getModel(); //mtt name of table
tree++; //increment S.No
model.addRow(new Object[]{tree,proname.getText(),TFroo.getText(),qty.getText(),total.getText(),bn.getText()}); // getting text from text field and add it in table
// and some other code to add data on database
}//Add Button ActionPerformed
}//google
删除代码按钮
int p= JOptionPane.showConfirmDialog(null,"SURE YOU WANT TO DELETE ?","CONFORM",JOptionPane.YES_NO_OPTION);
if(p==JOptionPane.YES_OPTION)
{
model.removeRow(mtt.getSelectedRow());
// and some other code to delete data also form database
}
答案 0 :(得分:0)
只需逐步重新设置已删除行的值
if(p==JOptionPane.YES_OPTION)
{
int row = mtt.getSelectedRows()[0];// returns row position
model.removeRow(row);
for(int y=row ;y<model.getRowCount();y++){
model.setValueAt(y, y, 0); //setValueAt(data,row,column)
}
}
答案 1 :(得分:0)
if(p==JOptionPane.YES_OPTION)
{
int row = mtt.getSelectedRows()[0];// returns row position(Here mtt is name of table)
model.removeRow(row);
for(int index=row ;index<model.getRowCount();index++){
model.setValueAt(index+1, index, 0); //setValueAt(data,row,column)
}
}