gpt4 book ai didi

java - 如何捕获JTree节点展开事件

转载 作者:行者123 更新时间:2023-12-02 04:28:58 24 4
gpt4 key购买 nike

我在SWING JTree中寻找一种类似于SWT的方法TreeNode.addListener(SWT.Expand, new Listener()...我设法检查最后一个选定的节点以找到选定的节点,但如何找到扩展的节点并以正确的方式处理它?<​​/p>

最佳答案

使用(有趣的命名)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);

关于java - 如何捕获JTree节点展开事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com