简体   繁体   English

获取所选JTable行中所有列的值

[英]Get values of all columns in selected JTable row

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). 所以我有一个应用程序,其中我有一个JTable,其中包含与计算机上的进程列表相关的值(因此它具有进程名称,PID,内存偏移等内容)。 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? 作为其中的一部分,我想在用户点击特定进程的行时收集进程名称和PID - 但我该如何做? 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. 如果我选择一行调用“table.getSelectedRows()”或“table.getSelectedColumns()”,我只得到一个表示单击字段的列或行索引的元素。 Thanks for any help. 谢谢你的帮助。

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. 您可以通过为每列调用table.getValueAt(row,column)一次来获取行中每个单元格的数据,其中'row'是选定的行索引,'column'是列的从零开始的索引。

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. 请注意,这可能会有些问题,因为用户可以重新排序列,并且此方法以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). 更好的方法是通过table.getModel()引用JTable的TableModel,然后使用TableModel的getValueAt()方法以模型顺序获取列(在视图中重新排序列时不会更改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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