开发者

Resequence table data after deletion of row in JTable

开发者 https://www.devze.com 2023-02-11 21:36 出处:网络
I have a JTable with 3 column of which first column is a button and second column is id. Now when user clicks on the button of specific row that row should be deleted. Up to this point everything wo$r

I have a JTable with 3 column of which first column is a button and second column is id. Now when user clicks on the button of specific row that row should be deleted. Up to this point everything wo$rks fine. But another requirement is re-sequencing the table data. I mean the column id should be re-sequence. For example:

First table data:

   | id | another-column
b1 | 1  | abc
b2 | 2  | xyz
b3 | 3  | def
b4 | 4  | qwe

Now when user deletes second row which have id=2 then table data should be as follows:

   | id | another-column
b1 | 1  | abc
b3 | 2  | def
b4 | 3  | qwe

Here b1/b2/b3/b4 are buttons for deletion of particul开发者_StackOverflow中文版ar row.

How can I do this?


It is fairly easy to set this sequential id value inside the getValueAt(row, col) of your Abstract Table Model.

public Object getValueAt(int row, int col) {
   if (col == 1) 
      return int (row + 1);
      .....
}

Set the model again inside the action event of your buttons. The model will repaint the JTable correctly after a button has been pressed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号