如何使用 DefaultMutableTreeNode、JTree 获得文件夹结构
how to have Folder Structure with DefaultMutableTreeNode,JTree
我试图显示 folder/subfolder 与 parent/children
相同的文件夹结构
假设这个结构:
但是我正在为如何将子节点添加到父节点而苦苦挣扎,我有一个名为 Computerscience 的父文件夹,我获取了它的所有子文件夹并将它们添加为子文件夹,但是当有它们里面有更多的子文件夹,看来我需要一个递归函数,但我做不到,这是我所做的:
import java.awt.BorderLayout;
import java.io.File;
import java.util.Arrays;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TestTree extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public TestTree() {
super("Tree Test Example");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
protected String[] getListFiles(String Path) {
File file = new File(Path);
String[] names = file.list();
return names;
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computerscience");
String[] names = this.getListFiles("C:\Users\neginn\Google Drive\NetBeansProjects\computerscience");
for (String name : names) {
root.add(new DefaultMutableTreeNode(name));
String[] names_level_2 = this.getListFiles("C:\Users\neginn\Google Drive\NetBeansProjects\computerscience\" + name);
if (names_level_2 != null) {
for (String name_level2 : names_level_2) {
int folder_index = Arrays.asList(names_level_2).indexOf(name_level2);
treeModel.insertNodeInto(root, (DefaultMutableTreeNode) root.getChildAt(folder_index), folder_index);
}
}
}
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
TestTree tt = new TestTree();
tt.init();
tt.setVisible(true);
}
}
我的问题在行 treeModel.insertNodeInto
正如你所说,你需要写一个递归方法。我试着写了一个,请测试下面的代码:
public class TestTree extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public TestTree() {
super("Tree Test Example");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
protected File[] getListFiles(String Path) {
File file = new File(Path);
return file.listFiles();
}
private void addChilds(DefaultMutableTreeNode rootNode, String path) {
File[] files = this.getListFiles(path);
for(File file:files) {
if(file.isDirectory()) {
DefaultMutableTreeNode subDirectory = new DefaultMutableTreeNode(file.getName());
addChilds(subDirectory, file.getAbsolutePath());
rootNode.add(subDirectory);
} else {
rootNode.add(new DefaultMutableTreeNode(file.getName()));
}
}
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computerscience");
addChilds(root, "C:\Users\neginn\Google Drive\NetBeansProjects\computerscience");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
TestTree tt = new TestTree();
tt.init();
tt.setVisible(true);
}
}
我试图显示 folder/subfolder 与 parent/children
相同的文件夹结构假设这个结构:
但是我正在为如何将子节点添加到父节点而苦苦挣扎,我有一个名为 Computerscience 的父文件夹,我获取了它的所有子文件夹并将它们添加为子文件夹,但是当有它们里面有更多的子文件夹,看来我需要一个递归函数,但我做不到,这是我所做的:
import java.awt.BorderLayout;
import java.io.File;
import java.util.Arrays;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TestTree extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public TestTree() {
super("Tree Test Example");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
protected String[] getListFiles(String Path) {
File file = new File(Path);
String[] names = file.list();
return names;
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computerscience");
String[] names = this.getListFiles("C:\Users\neginn\Google Drive\NetBeansProjects\computerscience");
for (String name : names) {
root.add(new DefaultMutableTreeNode(name));
String[] names_level_2 = this.getListFiles("C:\Users\neginn\Google Drive\NetBeansProjects\computerscience\" + name);
if (names_level_2 != null) {
for (String name_level2 : names_level_2) {
int folder_index = Arrays.asList(names_level_2).indexOf(name_level2);
treeModel.insertNodeInto(root, (DefaultMutableTreeNode) root.getChildAt(folder_index), folder_index);
}
}
}
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
TestTree tt = new TestTree();
tt.init();
tt.setVisible(true);
}
}
我的问题在行 treeModel.insertNodeInto
正如你所说,你需要写一个递归方法。我试着写了一个,请测试下面的代码:
public class TestTree extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public TestTree() {
super("Tree Test Example");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
protected File[] getListFiles(String Path) {
File file = new File(Path);
return file.listFiles();
}
private void addChilds(DefaultMutableTreeNode rootNode, String path) {
File[] files = this.getListFiles(path);
for(File file:files) {
if(file.isDirectory()) {
DefaultMutableTreeNode subDirectory = new DefaultMutableTreeNode(file.getName());
addChilds(subDirectory, file.getAbsolutePath());
rootNode.add(subDirectory);
} else {
rootNode.add(new DefaultMutableTreeNode(file.getName()));
}
}
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Computerscience");
addChilds(root, "C:\Users\neginn\Google Drive\NetBeansProjects\computerscience");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
TestTree tt = new TestTree();
tt.init();
tt.setVisible(true);
}
}