如何捕捉 JTree 节点扩展事件

How to catch JTree node expanded event

我在SWING JTree中寻求一种类似于SWT的方法 TreeNode.addListener(SWT.Expand, 新的监听器()... 我设法检查最后选定的节点以找到选定的节点,但我如何才能找到扩展的节点并以正确的方式处理它?

使用(有趣的名字)TreeWillExpandListener

TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
      public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent)
          throws ExpandVetoException {

        TreePath path = treeExpansionEvent.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();

        //Print the name of the node if toString() was implemented
        String data = node.getUserObject().toString();
        System.out.println("WillCollapse: " + data);

      }

      public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {

        TreePath path = treeExpansionEvent.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();

        //print the name of the node if toString was implemented  
        String data = node.getUserObject().toString();
        System.out.println("WillExpand: " + data);

      }
    };

tree.addTreeWillExpandListener(treeWillExpandListener);