简体   繁体   English

Java Swing:扩展TreeNode

[英]Java Swing: Expanding TreeNode

I am working on customizing a closed source client application. 我正在自定义封闭源客户端应用程序。 It has a tree in the UI and exposed only a method to get the selected node. 它在用户界面中有一棵树,并且仅公开了一种获取所选节点的方法。 It returns a subclass of TreeNode. 它返回TreeNode的子类。 And there is no way to get a reference to the parent tree. 而且没有办法获得对父树的引用。 Now i want to expand the selected node upto its leaves. 现在,我想将所选节点扩展到其叶子。

Is there any way to get a reference to the JTree component from a DefaultMutableTreeNode? 有什么方法可以从DefaultMutableTreeNode获取对JTree组件的引用吗? I am planning to use the JTree.expandPath() but I only have the reference to the treenode. 我打算使用JTree.expandPath(),但仅具有对treenode的引用。

I'm new to Swing and any suggestions to achieve this are welcome. 我是Swing的新手,欢迎提出任何建议。

Actually, only the JTree is aware of the expansion state, there is no such information in the TreeNodes. 实际上,只有JTree知道扩展状态,TreeNode中没有此类信息。 The TreeNodes are on the "model" side, and have no pointer on the JTree (at least not default ones, you can of course make your own with a reference, but it breaks a bit the pattern). TreeNode位于“模型”侧,并且在JTree上没有指针(至少不是默认指针),您当然可以使用引用创建自己的指针,但会破坏模式。

You should reconsider the place where you want to make this expansion for a place in which you have access to the JTree (provide maybe more details about your context, to know more what you want to do, and in which circumstances). 您应该重新考虑要扩展到可以访问JTree的位置的位置(可能提供有关上下文的更多详细信息,以了解更多您想做的事情,以及在什么情况下)。

Looking at the source for DefaultMutableTreeNode , I can't see any nice way to do it, but you could try one of the following: 在查看DefaultMutableTreeNode的源代码时,我看不到任何不错的方法,但是您可以尝试以下方法之一:

  • Extend DefaultMutableTreeNode and store the reference to your tree 扩展DefaultMutableTreeNode并存储对树的引用
  • The default node has a concept of a "user object". 默认节点具有“用户对象”的概念。 You could set the reference to the tree as this object. 可以将对树的引用设置为此对象。 It's completely un-type safe, but would probably work. 这是完全非类型安全的,但可能会起作用。 EDIT: Actually, looking closer at how user object is used (as a textual representation of the tree path and in toString() ), setting the user object in this way would be massacring the API's intended use. 编辑:实际上,仔细查看用户对象的使用方式(作为树路径和toString()的文本表示形式),以这种方式设置用户对象将破坏API的预期用途。 So: nasty, but might work. 所以:令人讨厌,但可能会起作用。

The tree node is an element from the tree model. 树节点是树模型中的元素。 As a consequence, it doesn't have any reference to the Tree that is its view. 因此,它没有对作为其视图的树的任何引用。

What could be interesting is to know in which object you are : since you want to react to a mouse event, I guess you have access to the view layer (at least by getting back the component originating the MouseEvent, which should be the JTree). 有趣的是知道您在哪个对象中:由于您想对鼠标事件做出反应,因此我想您可以访问视图层(至少通过取回起源于MouseEvent的组件(应该是JTree)) 。 If so, you can get the selected row and call expandPath . 如果是这样,您可以获取选定的行并调用expandPath Building the TreePath from the DefaultMutableTreeNode is left as an exercise :-) 从DefaultMutableTreeNode构建TreePath作为练习:-)

EDIT From the below comments, let me clarify my answer. 编辑从下面的评论中,让我澄清我的答案。 You'll have to call expandPath using a TreePath generated by simply calling getPath on your DefaultMutableTreeNode. 您必须使用通过简单地在DefaultMutableTreeNode上调用getPath生成的TreePath来调用expandPath

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

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