JTree中的桌面视图-Swing-仅Windows [英] Desktop view in JTree - Swing - Windows Only
问题描述
我想在JTree
中获得桌面视图,如下所示:
I would like to get a desktop view in JTree
, something like this:
我有一个示例代码,其中显示了因为我刚接触Java,所以只有c:\
很难实现.
I have a sample code which shows only the c:\
, since I am new to Java, finding difficulties to achieve it.
到目前为止,这是我的代码:
Here is my code so far:
public class FileTreeDemo {
public static void main(String[] args) {
File root;
if (args.length > 0) {
root = new File(args[0]);
} else {
root = new File(System.getProperty("user.home"));
}
System.out.println(root);
FileTreeModel model = new FileTreeModel(root);
JTree tree = new JTree();
tree.setModel(model);
tree.setRootVisible(true);
tree.setShowsRootHandles(true);
JScrollPane scrollpane = new JScrollPane(tree);
JFrame frame = new JFrame("FileTreeDemo");
frame.getContentPane().add(scrollpane, "Center");
frame.setSize(400,600);
frame.setVisible(true);
}
}
FileTreeModel类
class FileTreeModel implements TreeModel {
protected File root;
public FileTreeModel(File root) { this.root = root; }
public Object getRoot() { return root; }
public boolean isLeaf(Object node) { return ((File)node).isFile(); }
public int getChildCount(Object parent) {
String[] children = ((File)parent).list();
if (children == null) return 0;
return children.length;
}
public Object getChild(Object parent, int index) {
String[] children = ((File)parent).list();
if ((children == null) || (index >= children.length)) return null;
return new File((File) parent, children[index]);
}
public int getIndexOfChild(Object parent, Object child) {
String[] children = ((File)parent).list();
if (children == null) return -1;
String childname = ((File)child).getName();
for(int i = 0; i < children.length; i++) {
if (childname.equals(children[i])) return i;
}
return -1;
}
public void valueForPathChanged(TreePath path, Object newvalue) {}
public void addTreeModelListener(TreeModelListener l) {}
public void removeTreeModelListener(TreeModelListener l) {}
}
到目前为止,我已经尝试过更改系统属性",但是它不起作用:
SO far I have tried to change 'System Properties' but it didn't work:
"user.dir"
请告诉我一些指导,谢谢.
Please show me show me some directions, thanks.
推荐答案
If I understand your requirements correctly, then you need to use FileSystemView class in order to get OS specific data such as root partitions. Since the JDK1.1 File
API doesn't allow access OS related information.
注意:在Windows Desktop
文件夹中,该文件夹被视为根节点.例如,在Windows上运行,以下代码片段应打印出您在桌面上看到的文件夹,就像您所包含的图片一样:
Note: in Windows Desktop
folder is considered a root node. For example, running on Windows the following snippet should print the folders that you see in your desktop, pretty much like the picture you've included:
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
for (File file : fileSystemView.getRoots()) {
System.out.println("Root: " + file);
for (File f : file.listFiles()) {
if (f.isDirectory()) {
System.out.println("Child: " + f);
}
}
}
您可以使用以下方法为每个节点设置正确的图标:
You can use the following methods to set the correct icon for each node:
- isComputerNode(File file)
- isDrive(File file)
- isFloppyDrive(File file)
- isFileSystem(File file)
这篇关于JTree中的桌面视图-Swing-仅Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!