blejzz
blejzz

Reputation: 3349

java JTable how to track rows

I have a JTable which is connected to sqlite. The db table looks like this:

 resource_id #primary_key, file, type

I have already implemented adding the rows from db, but the problem is i need to know the resource id when some row in jTable is selected (not the index). Is there a way to add rows with unique ids and not based on indexes (or something similar)?

The current solution adds the resource id as a table column, but that doesnt solve the problem completely.

Upvotes: 0

Views: 294

Answers (1)

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

Create a class say TableData that contains the data from the table. Use a custom TableModel and place the data for the JTable in Vector<TableData>.

You may find it useful to create a method such as addRow(TableData data) in your TableModel that process the data from the table and adds data to the Vector.

In the overridden method public removeRow(int row) you will need to remove the vector data where row can serve as the index.

The overridden method public Object getValueAt(int row, int col) which is used to display the data in the JTable will then just need to retrieve the data from the Vector<TableData>. You can also place the logic for other columns which not part of the TableData in this method.

Dont forget to call fireTableRowsUpdated(row,col) and fireTableCellUpdated(row,col) where ever applicable.

For further reference and how to handling the selections in JTable you can refer this tutorial

Upvotes: 2

Related Questions