简体   繁体   English

如何更改JTree中空文件夹的图标? (FTP文件)

[英]How to change icon of a empty folder in JTree? (FTP File)

I am working with to show all files and folders of a FTPserver into a JTree. 我正在努力将FTPserver的所有文件和文件夹显示到JTree中。 But I got a problem that the empty folders are shown as a file. 但是我有一个问题,空文件夹显示为文件。 But how to show them as a folder icon? 但是如何将它们显示为文件夹图标?

Here is my code: 这是我的代码:

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();
    }   
} 

FTP J树

The "sad" folder is a empty folder, but shown as a file icon. “ sad”文件夹是一个空文件夹,但显示为文件图标。 How to change it? 怎么改变呢?

Thank you very much 非常感谢你

PS: Same of the methods is not working, like PS:相同的方法无法正常工作,例如

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

Because we are working with FTP files not with files. 因为我们使用的是FTP文件,而不是文件。

You have two choices: 您有两种选择:

I think the second coice is preferable. 我认为第二个建议是可取的。 I also recommend to use your own TreeModel to indicate if the tree node is a file or folder. 我还建议使用您自己的TreeModel来指示树节点是文件还是文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM