10

如何双击 JTree 节点并获取其名称?

如果我调用evt.getSource()它,返回的对象似乎是一个 JTree。我不能将它转换为 DefaultMutableTreeNode。

4

4 回答 4

25

From the Java Docs

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.

This returns an Object, so you will need to cast back to the required node type yourself.

于 2012-10-11T20:30:19.907 回答
11

以下代码适用于我。

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
            }
        }
    });
于 2016-02-17T16:29:48.680 回答
3

MadProgrammer 几乎涵盖了所有内容。要获取您可以调用的对象

DefaultMutableTreeNode selectedNode =
   ((DefaultMutableTreeNode)selPath.getLastPathComponent()).
   getUserObject();
于 2012-10-12T00:11:56.570 回答
1

我的例子。我们可以检测到有延迟的双击。

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=");      

    }     
}
于 2014-10-22T08:12:47.603 回答