简体   繁体   中英

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. 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. How to change it?

Thank you very much

PS: Same of the methods is not working, like

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

Because we are working with FTP files not with files.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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