简体   繁体   English

在JTree中获取节点

[英]Getting a node in JTree

Simple question. 简单的问题。 I have a TreePath to a node in my JTree. 我有一个TreePath到我的JTree中的节点。 How can I convert this TreePath to the DefaultMutableTreeNode the TreePath points too? 如何将此TreePath转换为TreePath指向的DefaultMutableTreeNode?

You should be able to call getLastPathComponent on the TreePath and cast that for a TreeNode or DefaultMutableTreeNode and be good to go. 您应该能够在TreePath上调用getLastPathComponent并为TreeNodeDefaultMutableTreeNode并且很好。

See: http://download.oracle.com/javase/6/docs/api/javax/swing/tree/TreePath.html#getLastPathComponent%28%29 请参阅: http//download.oracle.com/javase/6/docs/api/javax/swing/tree/TreePath.html#getLastPathComponent%28%29

如果你的treemodel由DefaultMutableTreeNodes组成,你可以使用node=(DefaultMutableTreeNode)path.getLastPathComponent();

model is a DefaultTreeModel model是DefaultTreeModel

private TreePath getTreePath(TreeNode node) {
    TreeNode[] nodes = model.getPathToRoot(node);
    TreePath path = new TreePath(nodes);
    return path;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM