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 mouseClicked(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);
NOTE: This example obtains both the path and row, but you only need to get the one you're interested in.
本文介绍了一种在Java中为JTree组件添加鼠标监听的方法,通过监听单击和双击事件,可以实现不同功能的调用。具体实现了如何获取点击位置对应的节点路径及行号,并根据点击次数触发相应的处理函数。
3万+

被折叠的 条评论
为什么被折叠?



