简体   繁体   English

使用具有完整内容的单个节点元素显示JTree

[英]Display JTree using a single node element having full content

I have a single node that has all the child nodes and attributes in the following format. 我有一个具有以下格式的所有子节点和属性的节点。

node =   Root[
         attributes = {rootattribute1, rootattribute2,...}, 
         value = [100,
                childNode1
                [
                attributes = {childNode2att1,.....}
                value = [1001]  
                ]

                childNode2
                [
                attributes = {childNode2attributes,.....}
                value = [1001]  
                ] ......... and some other childnodes like this
                ]

When I use Jtree tree = new Jtree(node); 当我使用Jtree tree = new Jtree(node); It is creating just a single rootelement for the tree showing all these details within a single row of a tree. 它只为树创建一个单一的rootelement,在树的单行中显示所有这些详细信息。

Instead I want to display the tree in the correct hierarchy with nested child nodes and atrribute values. 相反,我想以正确的层次结构显示树,并带有嵌套的子节点和atrribute值。 Is there any inbuilt method to do this ? 有任何内置方法可以做到这一点吗?

If there is no inbuilt method to do this how do I write the code for this ? 如果没有内置方法可以执行此操作,该如何编写代码?

PS: the node content displayed above is dynamic and is not static. PS:上面显示的节点内容是动态的,不是静态的。

You can start with something like: 您可以从以下内容开始:

import javax.swing.*
import javax.swing.tree.*

class Root {
    def attributes = []
    def children = []
    def value = 0

    def String toString() { 
        "[${value}] attributes: ${attributes} children: ${children}" 
    }
}

def createTreeNode(node) {
    def top = new DefaultMutableTreeNode(node.value)
    for (attr in node.attributes) {
        top.add(new DefaultMutableTreeNode(attr))
    }
    for (child in node.children) {
        top.add(createTreeNode(child))
    }   
    top
}

root = new Root(
    attributes: ['rootattribute1', 'rootattribute2'], 
    value: 100,
    children: [
        new Root(
            attributes: ['childNode2att1'],
            value: 1001),
        new Root(
            attributes: ['childNode2attributes'],
            value: 1002),    
    ])


frame = new JFrame('Tree Test')
frame.setSize(300, 300)
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
jtree = new JTree(createTreeNode(root))
frame.add(jtree)
frame.show()

JTree is a sophisticated component - please read the JTree Swing Tutorial for more details on how to customize the tree for your exact needs. JTree是一个复杂的组件-请阅读JTree Swing教程,以获取有关如何根据您的确切需求自定义树的更多详细信息。

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

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