gpt4 book ai didi

java - 如何使 JTable 上的左键单击操作适应 Java 中的右键单击?

转载 作者:行者123 更新时间:2023-11-29 07:14:04 26 4
gpt4 key购买 nike

我有一个 JTable,我想在其上左键单击并右键单击 JPopupMenu。通常通过左键单击 JTable,您可以选择一行。我想通过右键单击并显示弹出菜单来做同样的事情。有人知道怎么做吗?

table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
//this line gives wrong result because table.getSelectedRow() stay alwase on the same value
codeModel.setSelectedFileName(table.getValueAt(table.getSelectedRow(), 0).toString());
JPopupMenu popup = createRightClickPopUp();
popup.show(e.getComponent(), e.getX(), e.getY());
}else{
codeModel.setSelectedFileName(table.getValueAt(table.getSelectedRow(), 0).toString());
codeTextArea.setText(codeModel.getCodeContents());
}
}
});

最佳答案

table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) { //or mouseReleased(MouseEvent e)
if (SwingUtilities.isRightMouseButton(e)) {
//-- select a row
int idx = table.rowAtPoint(e.getPoint());
table.getSelectionModel().setSelectionInterval(idx, idx);
//---
codeModel.setSelectedFileName(table.getValueAt(table.getSelectedRow(), 0).toString());
JPopupMenu popup = createRightClickPopUp();
popup.show(e.getComponent(), e.getX(), e.getY());
}else{
codeModel.setSelectedFileName(table.getValueAt(table.getSelectedRow(), 0).toString());
codeTextArea.setText(codeModel.getCodeContents());
}
}
});

关于java - 如何使 JTable 上的左键单击操作适应 Java 中的右键单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11262925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com