【问题标题】:How to change icon of a empty folder in JTree? (FTP File)如何更改 JTree 中空文件夹的图标? (FTP文件)
【发布时间】:2012-05-06 05:55:29
【问题描述】:

我正在使用将 FTP 服务器的所有文件和文件夹显示到 JTree 中。但是我遇到了一个问题,即空文件夹显示为文件。但是如何将它们显示为文件夹图标?

这是我的代码:

public void buildTree(){

    try {
        ftpClient.connect("130.229.178.31");             
        ftpClient.login("admin", "123456");

        root = new DefaultMutableTreeNode("Welcome!");        
        for (int i = 0; i < 1; i++) {
            DefaultMutableTreeNode temp = new DefaultMutableTreeNode("FTP-Server");
            root.add(temp);
            bind(temp,"");
        }   
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException("Client Error", e1);
    }
    try {
            ftpClient.disconnect();
    } catch (IOException e2) {
            e2.printStackTrace();
            throw new RuntimeException("Error when shutdown", e2);
    }
}   

// bind nod/subnode to the tree (recursive method)
public void bind(DefaultMutableTreeNode node,String path){
    try {

        Boolean defaultPath = true;
        while (defaultPath)
        {
            defaultPath = ftpClient.changeToParentDirectory();
        }

        ftpClient.changeWorkingDirectory(path);

        FTPFile[] files = ftpClient.listFiles();


        for(int i=0;files!=null && i<files.length;i++){
            FTPFile tempFile = files[i];
            if(tempFile.isDirectory()){




                DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(tempFile.getName());
                node.add(tempNode);


                bind(tempNode, path+"/"+tempFile.getName());
            }else{
                DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(tempFile.getName());
                node.add(tempNode);
            }
        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
} 

“sad”文件夹是一个空文件夹,但显示为文件图标。怎么改?

非常感谢

PS: 同样的方法不起作用,比如

FileSystemView fileSystemView = FileSystemView.getFileSystemView();
setIcont(fileSystemView.getSystemIcon(File file));

因为我们处理的是 FTP 文件而不是文件。

【问题讨论】:

    标签: java ftp icons jtree


    【解决方案1】:

    你有两个选择:

    • 将“虚拟文件”添加到空文件夹(可能称其为“[empty]”)或
    • 使用你自己的DefaultTreeCellRenderer

    我认为第二个 coice 更可取。我还建议使用您自己的 TreeModel 来指示树节点是文件还是文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多