gpt4 book ai didi

java - 将数据库表转换为 JTree

转载 作者:行者123 更新时间:2023-12-01 15:20:01 25 4
gpt4 key购买 nike

任何人都可以提供一个工作示例代码来将以下具有父子关系的 Oracle DB 表转换为 Java 代码中的 JTree 吗?

表 T(列:Id、Parent_Id、描述)。

我搜索了许多类似的问题,但对于 JTree 的新手来说,所有问题似乎都很模糊且不完整。我不需要任何代码来连接到数据库,只需检查结果并填充 JTree。

谢谢

最佳答案

当我开始使用 JTree 时,我实现了这个类,它看起来与您的问题非常相似。

检查方法:getElementTreeFromPlainList 或多或少是您用来从持久层获取数据的方法,以及为树创建节点的方法createTreeNodesForElement

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class MyJFrame extends JFrame {

private static final long serialVersionUID = 1L;

private static List<String[]> data = new ArrayList<String[]>();

static {
data.add(new String[] { "Id1", "Start 1", null });
data.add(new String[] { "Id2", "Node 1.1", "Id3" });
data.add(new String[] { "Id3", "Node 1", "Id1" });
data.add(new String[] { "Id4", "Node 1.2", "Id3" });
data.add(new String[] { "Id5", "Node 2", "Id1" });
data.add(new String[] { "Id6", "Start 2", null });
}

public static void main(final String[] args) {
new MyJFrame();
}

MyJFrame() {
JTree tree = new JTree();

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Found Nodes");

createTreeNodesForElement(root, getElementTreeFromPlainList());

tree.setModel(new DefaultTreeModel(root));

add(tree);

setVisible(true);
setBounds(0, 0, 200, 200);
}

Collection<Element> getElementTreeFromPlainList() {
// builds a map of elements object returned from store
Map<String, Element> values = new HashMap<String, Element>();
for (String[] s : data) {
values.put(s[0], new Element(s[2], s[1]));
}

// creates a result list
Collection<Element> result = new ArrayList<MyJFrame.Element>();

// for each element in the result list that has a parent, put it into it
// otherwise it is added to the result list
for (Element e : values.values()) {
if (e.parent != null) {
values.get(e.parent).getChildren().add(e);
} else {
result.add(e);
}
}

return result;
}

void createTreeNodesForElement(final DefaultMutableTreeNode dmtn, final Collection<Element> elements) {
// for each element a tree node is created
for (Element child : elements) {
DefaultMutableTreeNode created = new DefaultMutableTreeNode(child.getName());
dmtn.add(created);
createTreeNodesForElement(created, child.getChildren());
}
}

public static class Element {
private final String parent;
private final String name;
private final Collection<Element> children = new ArrayList<MyJFrame.Element>();

public Element(final String parent, final String name) {
super();
this.parent = parent;
this.name = name;
}

public String getName() {
return name;
}

public Collection<Element> getChildren() {
return children;
}

}

}

关于java - 将数据库表转换为 JTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11066786/

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