任何人都可以提供一个工作示例代码,将以下带有父子关系的Oracle DB表转换为Java代码中的JTree吗?
Table T (columns: Id, Parent_Id, Description).
我搜索了许多类似的问题,但对于刚接触JTree的人来说,所有问题看起来都模糊不清。我不需要任何代码连接到数据库,但只需要检查结果并填充JTree。
由于
答案 0 :(得分:1)
当我开始使用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;
}
}
}