private void createNodes(DefaultMutableTreeNode top) {
List <String> files = new ArrayList<String>();
//getFileNames() returns array list of file name with complete path
//eg C:/lanceTest1/directory1/sample.txt
files = getFileNames();
DefaultMutableTreeNode node = null;
for (String fileName : files) {
node = new DefaultMutableTreeNode(fileName);
top.add(node);
}
}
上面的功能会在窗口下面创建。
我想将其显示如下(例如,对于第一个文件),
The Java Series
|__C
|__lanceTest
|__directory1
|__PDxxxx_Splunk_Solution_Architecture_Doc_v0.9.doc
我是新手摇摆,awt。有人可以帮忙吗?
答案 0 :(得分:5)
来自代码
import java.awt.*;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
public class FileTree extends JPanel {
private static final long serialVersionUID = 1L;
public FileTree(File dir) {
setLayout(new BorderLayout());
JTree tree = new JTree(addNodes(null, dir));
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
System.out.println("You selected " + node);
}
});
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
add(BorderLayout.CENTER, scrollpane);
}
private DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) {
curTop.add(curDir);
}
Vector<String> ol = new Vector<String>();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++) {
ol.addElement(tmp[i]);
}
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector<Object> files = new Vector<Object>();
for (int i = 0; i < ol.size(); i++) {
String thisObject = ol.elementAt(i);
String newPath;
if (curPath.equals(".")) {
newPath = thisObject;
} else {
newPath = curPath + File.separator + thisObject;
}
if ((f = new File(newPath)).isDirectory()) {
addNodes(curDir, f);
} else {
files.addElement(thisObject);
}
}
for (int fnum = 0; fnum < files.size(); fnum++) {
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
}
return curDir;
}
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 400);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
public static void main(final String[] av) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("FileTree");
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
Container cp = frame.getContentPane();
if (av.length == 0) {
cp.add(new FileTree(new File(".")));
} else {
cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
for (int i = 0; i < av.length; i++) {
cp.add(new FileTree(new File(av[i])));
}
}
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
答案 1 :(得分:2)
另请参阅File Browser GUI。
答案 2 :(得分:0)
感谢大家的帮助。基本上我想从我从数据库中检索的路径表示文件系统。文件/目录实际上并不存储在数据库中,而是从绝对/相对路径动态生成的。
我在这里找到了答案......
Java Tree to represent filesystem (files/dir) from list of paths