Double-click a JTree node and get its name
Asked Answered
M

4

12

How do I double-click a JTree node and get its name?

If I call evt.getSource() it seems that the object returned is a JTree. I can't cast it to a DefaultMutableTreeNode.

Mcconaghy answered 11/10, 2012 at 20:23 Comment(0)
J
26

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.

Jointworm answered 11/10, 2012 at 20:30 Comment(2)
Yes.I know this.But how do I use the selRow and selPath??Mcconaghy
Well the next thing you need to do is read the API entry for TreePathJointworm
M
12

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
            }
        }
    });
Mope answered 17/2, 2016 at 16:29 Comment(0)
D
3

MadProgrammer has pretty much everything covered. To get the object you can call

DefaultMutableTreeNode selectedNode =
   ((DefaultMutableTreeNode)selPath.getLastPathComponent()).
   getUserObject();
Degrease answered 12/10, 2012 at 0:11 Comment(1)
Maybe we should use it as Object and then cast: Object selectedNode = ( (DefaultMutableTreeNode) selPath.getLastPathComponent()).getUserObject();Gavriella
W
1

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

    }     
}
Wily answered 22/10, 2014 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.