我有一个包含不同文件路径的ArrayList,如下所示:
ArrayList<String> test = new ArrayList<String>();
test.add("/Test/blah.txt");
test.add("/Test/folder/1.txt");
test.add("/Test/folder/2.txt");
test.add("/Test/folder/3.txt");
test.add("/Test/folder/folder2/1.txt");
test.add("/Test/folder/folder2/2.txt");
test.add("/Test/folder/folder2/3.txt");
有没有办法从这个数组构建一个JTree?我已经尝试将此ArrayList转换为Vector或Hashtable。 但它永远不会按预期工作。
也许有人有个好主意来处理这个问题。 此致
答案 0 :(得分:0)
我正在为您的问题发布类似的代码。希望您觉得这有用....
public class Main {
public static void main(String[] args) {
JTree tree = new JTree(new TestModel());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(tree));
frame.pack();
frame.setVisible(true);
}
}
class TestModel implements TreeModel {
Element data;
TestModel() {
data = new Element("data");
data.add(new Element("One"));
data.add(new Element("Two"));
data.add(new Element("Three"));
data.add(new Element("Four"));
data.add(new Element("Five"));
}
@Override
public Object getRoot() {
return data;
}
@Override
public Object getChild(Object parent, int index) {
System.out.println("GetChild from " + parent + " @ " + index);
if (parent instanceof Element) {
Element p = (Element) parent;
Object child = p.getChildAt(index);
System.out.println("child = " + child);
return child;
}
return null;
}
@Override
public int getChildCount(Object parent) {
if (parent instanceof Element) {
Element e = (Element) parent;
System.out.println("childCount = " + parent + "; " + e.getChildCount());
return e.getChildCount();
}
return 0;
}
@Override
public int getIndexOfChild(Object parent, Object child) {
if (parent instanceof Element && child instanceof Element) {
Element e = (Element) parent;
System.out.println("indexOf " + child + " in " + parent + " is "
+ e.getIndex((Element) child));
return e.getIndex((Element) child);
}
return -1;
}
@Override
public boolean isLeaf(Object node) {
if (node instanceof Element) {
Element e = (Element) node;
System.out.println("isLeaf " + e + "; " + (e.getChildCount() == 0));
return e.getChildCount() == 0;
}
return true;
}
@Override
public void valueForPathChanged(TreePath path, Object newValue) {
}
@Override
public void addTreeModelListener(TreeModelListener l) {
}
@Override
public void removeTreeModelListener(TreeModelListener l) {
}
}
class Element implements TreeNode {
List<Element> nodes;
Element parent;
String name;
public Element(String n) {
nodes = new ArrayList<>(25);
name = n;
}
@Override
public String toString() {
return name;
}
protected void setParent(Element parent) {
this.parent = parent;
}
public void add(Element node) {
node.setParent(this);
nodes.add(node);
}
public void remove(Element node) {
node.setParent(null);
nodes.remove(node);
}
@Override
public TreeNode getChildAt(int childIndex) {
return nodes.get(childIndex);
}
@Override
public int getChildCount() {
return nodes.size();
}
@Override
public TreeNode getParent() {
return parent;
}
@Override
public int getIndex(TreeNode node) {
return nodes.indexOf(node);
}
@Override
public boolean getAllowsChildren() {
return true;
}
@Override
public boolean isLeaf() {
return nodes.isEmpty();
}
@Override
public Enumeration children() {
return Collections.enumeration(nodes);
}
}
答案 1 :(得分:0)
这是接近你的问题但在数组中完成的另一个程序..
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
public class DICTest extends JPanel{
String[] str={"test", "test","test\\TP1","test\\TP1",
"test\\TP1\\TP2","test\\TP1\\TP2\\TP3","test\\T1"
};
String[] conn_name={"test1","test2","tp1_test1","tp1_test2","tp2_test1","tp3_test1","t1_test1"};
public DICTest(JFrame frame) {
DefaultMutableTreeNode master = new DefaultMutableTreeNode("Root");
JTree tree = new JTree(master);
JScrollPane scroll = new JScrollPane(tree);
this.setSize(200,400);
for(int i=0;i<str.length;i++){
root=DrawTree(str[i],root,conn_name[i]);
}
add(scroll);
}
public static DefaultMutableTreeNode DrawTree(String directory, DefaultMutableTreeNode parent, String conn_name) {
DefaultMutableTreeNode node =new DefaultMutableTreeNode();
DefaultMutableTreeNode conn_Node =new DefaultMutableTreeNode();
System.out.println("Directory .... "+directory);
if(directory.equalsIgnoreCase("Connections")){
conn_Node = new DefaultMutableTreeNode(conn_name);
parent.add(conn_Node);
}else{
directory = directory.substring(directory.indexOf("\\")+1 ,directory.length());
System.out.println("New Directory in if .... "+directory);
if(directory.indexOf("\\")== -1){
node= findNode(directory,parent);
if(node==null){
node = new DefaultMutableTreeNode(directory);
parent.add(node);
}
conn_Node= new DefaultMutableTreeNode(conn_name);
node.add(conn_Node);
}else{
System.out.println("New Directory in else.... "+directory);
node= DrawTree( directory, node, conn_name);
}
}
return parent;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tree");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 1));
contentPane.add(new DICTest(frame));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
private static DefaultMutableTreeNode findNode(String s,DefaultMutableTreeNode root) {
// DefaultMutableTreeNode root =
// (DefaultMutableTreeNode)tree.getModel().getRoot();
Enumeration e = root.breadthFirstEnumeration();
while(e.hasMoreElements()) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)e.nextElement();
if(node.toString().equals(s)) {
return node;
}
}
return null;
}
}