从结构化为路径的字符串数组填充 JTree
Populate JTree from String array Structured as Paths
我的 Java 项目需要帮助。如何从模式化为路径的字符串数组动态填充我的 JTree?
比如,字符串
paths[][]={{"Animals", "Birds","Non_flying" ,"Chicken"},
{"Animals","Birds","Non_flying","Ostrich"},
{"Animals","Birds","Flying","Eagle"},
{"Animals","Birds","Flying","Crow"},
{"Animals","Reptiles","Lizard"},
{"Plants"," Fruit Bearing","Fruits","Mango"},
{"Plants"," Fruit Bearing","Vegetable","Eggplant"},
{"Plants"," Non-fruit Bearing","Sunflower"}};
Like this
我尝试了下面的代码,但它没有合并相似的节点。必须是treeify()方法里面的条件:
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeTest extends javax.swing.JFrame{
static JTree tree;
public static void setTree(){
String paths[][]={{"Animals", "Birds","Non_flying" ,"Chicken"},
{"Animals","Birds","Non_flying","Ostrich"},
{"Animals","Birds","Flying","Eagle"},
{"Animals","Birds","Flying","Crow"},
{"Animals","Reptiles","Lizard"},
{"Plants"," Fruit Bearing","Fruits","Mango"},
{"Plants"," Fruit Bearing","Vegetable","Eggplant"},
{"Plants"," Non-fruit Bearing","Sunflower"}};
tree = new JTree(treeify(paths));
}
public static <T> DefaultMutableTreeNode treeify(String[][] paths) {
DefaultMutableTreeNode root = null;
DefaultMutableTreeNode subRoot = null;
for ( String[] parent : paths)
for ( String value : parent){
if (root == null) {
root = new DefaultMutableTreeNode(value);
} else if (subRoot == null){
subRoot = new DefaultMutableTreeNode(value);
root.add(subRoot);
} else {
DefaultMutableTreeNode child = new DefaultMutableTreeNode(value);
subRoot.add(child);
subRoot = child;
}
}
return root;
}
public static void main(String[] args) {
TreeTest test = new TreeTest();
setTree();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.add(new JScrollPane(tree));
test.setSize(500,400);
test.setLocationRelativeTo(null);
test.setVisible(true);
}
}
这是 treeify()
的一个版本,它实现了我在评论中概述的策略。这不是有史以来最优雅的事情,但它完成了工作:
public static DefaultMutableTreeNode treeify(String[][] paths) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for ( String[] path : paths) {
DefaultMutableTreeNode curr = root;
for ( String value : path){
DefaultMutableTreeNode next = null;
Enumeration ce = curr.children();
while (ce.hasMoreElements()){
DefaultMutableTreeNode kid =
(DefaultMutableTreeNode) ce.nextElement();
if (((String)kid.getUserObject()).equals(value)){
next = kid;
break;
}
}
if (next == null){
next = new DefaultMutableTreeNode(value);
curr.add(next);
}
curr = next;
}
}
return root;
}
我的 Java 项目需要帮助。如何从模式化为路径的字符串数组动态填充我的 JTree? 比如,字符串
paths[][]={{"Animals", "Birds","Non_flying" ,"Chicken"},
{"Animals","Birds","Non_flying","Ostrich"},
{"Animals","Birds","Flying","Eagle"},
{"Animals","Birds","Flying","Crow"},
{"Animals","Reptiles","Lizard"},
{"Plants"," Fruit Bearing","Fruits","Mango"},
{"Plants"," Fruit Bearing","Vegetable","Eggplant"},
{"Plants"," Non-fruit Bearing","Sunflower"}};
Like this 我尝试了下面的代码,但它没有合并相似的节点。必须是treeify()方法里面的条件:
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeTest extends javax.swing.JFrame{
static JTree tree;
public static void setTree(){
String paths[][]={{"Animals", "Birds","Non_flying" ,"Chicken"},
{"Animals","Birds","Non_flying","Ostrich"},
{"Animals","Birds","Flying","Eagle"},
{"Animals","Birds","Flying","Crow"},
{"Animals","Reptiles","Lizard"},
{"Plants"," Fruit Bearing","Fruits","Mango"},
{"Plants"," Fruit Bearing","Vegetable","Eggplant"},
{"Plants"," Non-fruit Bearing","Sunflower"}};
tree = new JTree(treeify(paths));
}
public static <T> DefaultMutableTreeNode treeify(String[][] paths) {
DefaultMutableTreeNode root = null;
DefaultMutableTreeNode subRoot = null;
for ( String[] parent : paths)
for ( String value : parent){
if (root == null) {
root = new DefaultMutableTreeNode(value);
} else if (subRoot == null){
subRoot = new DefaultMutableTreeNode(value);
root.add(subRoot);
} else {
DefaultMutableTreeNode child = new DefaultMutableTreeNode(value);
subRoot.add(child);
subRoot = child;
}
}
return root;
}
public static void main(String[] args) {
TreeTest test = new TreeTest();
setTree();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.add(new JScrollPane(tree));
test.setSize(500,400);
test.setLocationRelativeTo(null);
test.setVisible(true);
}
}
这是 treeify()
的一个版本,它实现了我在评论中概述的策略。这不是有史以来最优雅的事情,但它完成了工作:
public static DefaultMutableTreeNode treeify(String[][] paths) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for ( String[] path : paths) {
DefaultMutableTreeNode curr = root;
for ( String value : path){
DefaultMutableTreeNode next = null;
Enumeration ce = curr.children();
while (ce.hasMoreElements()){
DefaultMutableTreeNode kid =
(DefaultMutableTreeNode) ce.nextElement();
if (((String)kid.getUserObject()).equals(value)){
next = kid;
break;
}
}
if (next == null){
next = new DefaultMutableTreeNode(value);
curr.add(next);
}
curr = next;
}
}
return root;
}