Question

so I have a application in which I have a JTable filled with values related to a process list on a computer (so it has things like process name, PID, memory offset, etc). As part of this I want to collect the process name and PID when a user clicks on a row for a certain process--but how do I do this? If I call "table.getSelectedRows()" or "table.getSelectedColumns()" with a row selected I just get one element representing the clicked field's column or row index. Thanks for any help.

Was it helpful?

Solution

You can get the data for each cell in the row by calling table.getValueAt(row, column) once for each column, with 'row' being the selected row index and the 'column' being the column's zero-based index.

Note that this can be somewhat problematic, however, because the user can re-order the columns, and this method references the column in display-order.

The better way to do this is to reference the JTable's TableModel via table.getModel(), and then using the TableModel's getValueAt() method to get the columns in model order (which does not change when the columns are re-ordered in the view).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top