java中我如何能通过addMouseListenr使得鼠标点击到JTable后将其一行中的数据提取出来?
时间: 2024-03-25 12:39:47 浏览: 18
您可以通过以下步骤实现此功能:
1. 为JTable添加MouseListener,重写mouseClicked方法。
2. 在mouseClicked方法中获取所选行的索引:
```
int selectedRow = table.getSelectedRow();
```
3. 通过索引获取所选行的数据:
```
Object[] rowData = new Object[table.getColumnCount()];
for (int i = 0; i < table.getColumnCount(); i++) {
rowData[i] = table.getValueAt(selectedRow, i);
}
```
rowData数组中包含了所选行的所有数据。您可以根据需要对其进行处理。
完整的示例代码如下:
```
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
int selectedRow = table.getSelectedRow();
Object[] rowData = new Object[table.getColumnCount()];
for (int i = 0; i < table.getColumnCount(); i++) {
rowData[i] = table.getValueAt(selectedRow, i);
}
// 处理rowData数组中的数据
}
}
});
```