Double-click a JTree node and get its name

后端 未结 4 509
忘掉有多难
忘掉有多难 2020-12-11 16:39

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 Defa

4条回答
  •  时光取名叫无心
    2020-12-11 17:10

    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
                }
            }
        });
    

提交回复
热议问题