gpt4 book ai didi

java - 如何在 DefaultMutableTreeNode 上获取鼠标监听器?

转载 作者:行者123 更新时间:2023-11-29 07:12:16 24 4
gpt4 key购买 nike

我知道我可以将 mouseListener 添加到 Jtree,但我需要双击他的元素之一并执行 doSomething();

最佳答案

来自documentation of JTree :

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/

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