我知道我可以将 mouseListener 添加到 Jtree,但我需要双击他的元素之一并执行 doSomething();
最佳答案
If you are interested in detecting either double-click events or when a user clicks on a node, regardless of whether or not it was selected, we recommend you do the following:
final JTree tree = ...;
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
mySingleClick(selRow, selPath);
}
else if(e.getClickCount() == 2) {
myDoubleClick(selRow, selPath);
}
}
}
};
tree.addMouseListener(ml);
关于java - 如何在 DefaultMutableTreeNode 上获取鼠标监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12846518/