如何在删除行表格时自动调整jtable中的序列号
how to automatically adjust serial number in jtable when delete on row form it
在上面的table中有4个值在table中用序列号表示(S.NO
1,2,3,4)
问题是当我们从 table 中删除一行时 S.NO 2
然后它使用 removeRow 方法删除一行但它不调整 S.No
S.NO 的代码就像我引入了一个名为 tree 的 int 变量,每当我们按下添加按钮时,它都会在 tree
中添加一个值
int tree=0;
当我们按下 ADD 按钮时
tree++;
我也试试
.getRowCount()
要获取行号,它会获取并打印它,但是当我们删除一行并添加另一行时,它会再次打印该行 number.So 如何调整 S.No
添加按钮代码(用于在 table 和数据库中添加数据)
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
}
只需增量地重新设置已删除行的值
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)
}
}
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)
}
}
在上面的table中有4个值在table中用序列号表示(S.NO 1,2,3,4)
问题是当我们从 table 中删除一行时 S.NO 2
然后它使用 removeRow 方法删除一行但它不调整 S.No S.NO 的代码就像我引入了一个名为 tree 的 int 变量,每当我们按下添加按钮时,它都会在 tree
中添加一个值int tree=0;
当我们按下 ADD 按钮时
tree++;
我也试试
.getRowCount()
要获取行号,它会获取并打印它,但是当我们删除一行并添加另一行时,它会再次打印该行 number.So 如何调整 S.No
添加按钮代码(用于在 table 和数据库中添加数据)
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
}
只需增量地重新设置已删除行的值
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)
}
}
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)
}
}