创建 JTree 到第 n 层

问题描述 投票:0回答:1

这可能很简单,但从过去几个小时开始,我一直坚持要根据数据库记录创建

JTree
nth
级别。 下面的代码将子级添加到
level 1
,我不知道如何将子级添加到
nth
级别,因为数据库带有子级的
parentID

private DefaultMutableTreeNode setSandboxJTreeModel() {
    try {
        sandboxJTreeRootNode = new DefaultMutableTreeNode(AppConstants.SANDBOX_TREE_NAME);
        //sandboxJTreeModel = new DefaultTreeModel(sandboxJTreeRootNode);
        ArrayList<tbl_bom_sandbox> sandboxArray = daoSandboxObject.fetchAllSandboxRoutes();
        
        DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode(sandboxArray.get(0).getRouteName());
        sandboxJTreeRootNode.add(parentNode);
        
        for (int item = 1; item < sandboxArray.size(); item++) {
            DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(sandboxArray.get(item).getInFeedItemName());
            parentNode.add(childNode);
        }
        
        
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sandboxJTreeRootNode;
}

图中代码的当前输出以及预期输出。

java jtree
1个回答
0
投票

数据结构与图片完全相同,树的深度会根据记录的数量而变化。

因此,从概念上讲,您希望将子节点追加到前一个子节点。

这可以使用简单的递归方法来完成,类似于......

protected void populate(DefaultMutableTreeNode parent, List<String> items, int index) {
    if (index >= items.size()) {
        return;
    }
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(items.get(index));
    parent.add(node);
    populate(node, items, ++index);
}

或者更直接地通过简单的

for-loop
...

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(items.get(0));
DefaultMutableTreeNode parentNode = rootNode;
for (int index = 1; index < items.size(); index++) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(items.get(index));
    parentNode.add(childNode);
    parentNode = childNode;
}

可运行示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new BorderLayout());
            JTree tree = new JTree();
            tree.setShowsRootHandles(true);

            List<String> items = new ArrayList<>(32);
            items.add("Bill of materials (BOM) SANDBOX");
            items.add("00550R1020AROI_Straightening");
            items.add("00815R1214ARPP");
            items.add("01400R1030ARDWN");
            items.add("01400R1030ARDWN");

            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(items.get(0));
            populate(rootNode, items, 1);

            DefaultTreeModel model = new DefaultTreeModel(rootNode);
            tree.setModel(model);

            add(new JScrollPane(tree));
        }

        protected void populate(DefaultMutableTreeNode parent, List<String> items, int index) {
            if (index >= items.size()) {
                return;
            }
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(items.get(index));
            parent.add(node);
            populate(node, items, ++index);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.