jtable获取选中行数据
时间: 2023-06-05 09:06:54 浏览: 799
可以使用以下代码来获取 JTable 中选中行的数据:
```
int row = table.getSelectedRow();
String col1 = table.getModel().getValueAt(row, 0).toString();
String col2 = table.getModel().getValueAt(row, 1).toString();
String col3 = table.getModel().getValueAt(row, 2).toString();
// ...
System.out.println("Selected row data: " + col1 + ", " + col2 + ", " + col3 + ", ...");
```
其中,`table` 是你的 JTable 实例,`row` 是选中的行数,`col1`、`col2`、`col3` 分别是选中行的第一、第二、第三列数据,你可以根据需要获取更多或更少的列数据。
相关问题
jtable获取选中行数据并在控制台打印
您好!关于您的问题,可以通过以下代码实现:
```java
// 获取选中行的索引
int selectedRowIndex = jtable.getSelectedRow();
// 如果没有选中行,退出函数
if (selectedRowIndex == -1) {
return;
}
// 获取选中行的数据
String data = "";
for (int i = 0; i < jtable.getColumnCount(); i++) {
Object value = jtable.getValueAt(selectedRowIndex, i);
data += value.toString() + " ";
}
// 在控制台打印选中行的数据
System.out.println(data);
```
希望这个回答对您有所帮助!若您有其他问题需要咨询,可以继续提出。
Jtable选中一行,怎么获取其中某列的数据
可以使用 JTable 的方法 `getValueAt(int row, int col)` 来获取指定行列的数据。具体步骤如下:
1. 获取选中行的索引:
```java
int selectedRowIndex = table.getSelectedRow();
```
2. 获取指定列的索引:
```java
int desiredColIndex = 3; // 假设要获取第4列的数据
```
3. 获取指定行列的数据:
```java
Object desiredData = table.getValueAt(selectedRowIndex, desiredColIndex);
```
这里的 `desiredData` 就是你想要获取的数据,可以根据需要进行强制类型转换。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)