2015-07-12 30 views
2

enter image description here如何自動調節的JTable序列號時對行刪除形成它

在上表中有在由序列號表示的表4的值(S.NO 1,2,3,4)

enter image description here

的問題是,當我們從表中刪除的行狀S.NO 2

enter image description here

然後使用removeRow方法刪除一行但不調整S.No S.NO的代碼就像我引入一個名爲tree的int變量,當我們按下add按鈕時它會在樹中添加一個值

int tree=0; 

,當我們按添加按鈕

tree++; 

,我也嘗試

.getRowCount() 

得到行號就可以獲得它和打印,也而T3再次母雞當我們刪除一行,並添加另一排它只是打印該行number.So如何調整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

只是重新設置刪除的行增量

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) 
     } 

} 
+0

感謝回覆:) –

+0

我添加代碼的添加按鈕和刪除按鈕看看 –

+0

@NeerajSharma,完成! –

0
的價值
+0

當從jtable中刪除行而沒有重複的序列號時,自動調整序列號 –