Hi all,

I'm new to JTable. I have two buttons "add Row", "Del Row" and I have some records displayed from db which contains an auto increment column. when ever "add row" is clicked a new record will be added to JTable with a auto increment column number displayed in sequential order.

My requirement is when ever a specific row in between is removed by clicking "del row" button. then the succeeding records auto increment column should be updated with removed column id and the sequence should be updated for the remaining columns. The same has to be updated in the database too.

please help me on this regard.

Thanks in advance,
Chandu

Recommended Answers

All 4 Replies

I don't believe that the database should be updated. Especially if that column is a Primary Key linked with some other table.
For the JTable:
When you click the row, you can get the selected row index. That is sequential. When you delete it that value that you have saved in a variable is the index of the next row. So you can write a loop from that index till the end where you take each row and reduce by one the value of the column you want.

If you want to update the DB, while you are looping take the primary key of the row from the JTable and update the DB table

Hi javaAddict,

Thanks for your reply. But what if the case like user selects multiple rows to delete ?

Thanks in advance,
Chandu

I don't believe that the database should be updated. Especially if that column is a Primary Key linked with some other table.
For the JTable:
When you click the row, you can get the selected row index. That is sequential. When you delete it that value that you have saved in a variable is the index of the next row. So you can write a loop from that index till the end where you take each row and reduce by one the value of the column you want.

If you want to update the DB, while you are looping take the primary key of the row from the JTable and update the DB table

Hi javaAddict,

Thanks for your reply. But what if the case like user selects multiple rows to delete ?

Thanks in advance,
Chandu

I am not very familiar with the API of JTable, but if you find a way to select multiple rows to delete then you should be able to find a way get all those ids. Then execute multiple deletes to the database or just one using the IN sql keyword.
As for the sequential index, you can still loop all the rows of the table an update the column

Thanks JavaAddict. I will try to implement the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.