给JTable添加鼠标右键监听器,点击后可弹出菜单,选择菜单中的“打开”就可输出所选行的第4列的内容,但是每次点击后不仅会输出本次点击的内容,还会顺序输出之前所有点击的内容,但是我只需要当前点击的内容,求大神指点如何修改。具体代码段如下及运行结果如下:
//为表格添加鼠标右键点击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//判断是否为鼠标的右键(BUTTON3)
if(e.getButton() == MouseEvent.BUTTON3) {
int focusedRowIndex = table.rowAtPoint(e.getPoint());
String filePath = (String) table.getValueAt(focusedRowIndex, 4);
System.out.print(focusedRowIndex+",");
if(focusedRowIndex == -1) {
return;
}
//将表格所选项设为当前右键点击的行
table.setRowSelectionInterval(focusedRowIndex, focusedRowIndex);
//弹出菜单
jpm.show(table,e.getX(),e.getY());
//打开
jm1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) throws ArrayIndexOutOfBoundsException{
System.out.println(filePath);
}
});
}
}
});
![图片说明](https://img-ask.csdn.net/upload/201905/07/1557197814_716701.png)