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.
Create a class say
TableDatathat contains the data from the table. Use a customTableModeland place the data for the JTable inVector<TableData>.You may find it useful to create a method such as
addRow(TableData data)in yourTableModelthat process the data from the table and adds data to theVector.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 theVector<TableData>. You can also place the logic for other columns which not part of theTableDatain 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
JTableyou can refer this tutorial