简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM