java 双击 JTree 节点并获取其名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12847904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double-click a JTree node and get its name
提问by Adrian Stamin
How do I double-click a JTree node and get its name?
如何双击 JTree 节点并获取其名称?
If I call evt.getSource()
it seems that the object returned is a JTree. I can't cast it to a DefaultMutableTreeNode.
如果我调用evt.getSource()
它似乎返回的对象是一个 JTree。我无法将其转换为 DefaultMutableTreeNode。
回答by MadProgrammer
From the Java Docs
来自Java 文档
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);
To get the nodes from the TreePath
you can walk the path or simply, in your case, use TreePath#getLastPathComponent
.
要从中获取节点,TreePath
您可以走路径,或者简单地,在您的情况下,使用TreePath#getLastPathComponent
.
This returns an Object
, so you will need to cast back to the required node type yourself.
这将返回Object
,因此您需要自己转换回所需的节点类型。
回答by Marc Valdivia
The following code works for me.
以下代码对我有用。
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject();
// Cast nodeInfo to your object and do whatever you want
}
}
});
回答by Ani
MadProgrammer has pretty much everything covered. To get the object you can call
MadProgrammer 几乎涵盖了所有内容。要获取您可以调用的对象
DefaultMutableTreeNode selectedNode =
((DefaultMutableTreeNode)selPath.getLastPathComponent()).
getUserObject();
回答by anymol
My example. We can detect Double-click with delay.
我的例子。我们可以检测到双击延迟。
public class TreeListener extends MouseAdapter{
private JTree _Tree;
private boolean singleClick = true;
private int doubleClickDelay = 300;
private Timer timer;
public TreeListener(JTree tree)
{
this._Tree = tree;
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
if (singleClick) {
singleClickHandler(e);
} else {
try {
doubleClickHandler(e);
} catch (ParseException ex) {
Logger.getLogger(ex.getMessage());
}
}
}
};
timer = new javax.swing.Timer(doubleClickDelay, actionListener);
timer.setRepeats(false);
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
singleClick = true;
timer.start();
} else {
singleClick = false;
}
}
private void singleClickHandler(ActionEvent e) {
System.out.println("-- single click --");
}
private void doubleClickHandler(ActionEvent e) throws ParseException {
System.out.println("-- double click -- id=");
}
}