hello guys. I would like to ask you a help of how i can remote server and view files located on it using JTree in java.

I built an application that has two sides. client and server and when I run it using localhost it's fine. but the problem is the codes to help my JTree to remote directories located on server while I use two computers as client and server. I used FileSystemModel.java class

Let's break the problem down. Trying to write & test everything in one go is just too hard.
Start by trying to load the local file system into a JTree.

This line help me to view file using JTree.

 private void user_treeMouseClicked(java.awt.event.MouseEvent evt) {                                       
        JTreevar = user_tree.getSelectionPath().toString().replaceAll("[\\[\\]]", "").replace(", ", "/");


    }       

Then I customized JTree's code in other to precise where exactly the files are located.

account ac=account.getVerifyUsername(username);
System.out.println("Username "+username);
employee empp=employee.getEmployee(ac.getEmp_obj().getEmpId());
department depp=department.getDepartment(empp.getDep_obj().getDepId());
System.out.println(depp.getDepName());
user_tree = new javax.swing.JTree();
user_tree.setModel(new FileSystemModel(new File("C:\\Documents and Settings\\Admin\\Desktop\\server\\"+depp.getDepName()+"\\"+username)));
user_tree.setBackground(new java.awt.Color(153, 255, 153));

user_tree.setBorder(new javax.swing.border.MatteBorder(null));

user_tree.setRootVisible(false);

user_tree.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        user_treeMouseClicked(evt);
    }
});

This path is for server where user's files will be located.

That code should load the local file system, given a File (line 7) that specifies a file system root.
I'm not sure that FileSystemModel will help you a lot here, other than as a template for you to create your own remote FTP version. If you look through the code of FileSystemModel you will see that there aren't many places where it queries the underlying file system, so all you need to do is to replace those with corresponding FTP queries.

Edited 1 Year Ago by JamesCherrill

ok it means that I must modify FileSystemModel? could you help me to help me the lines I can replace and also FTP queries I can use?
This is FileSystemModel class am using:

import java.io.File;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Admin
 */
public class FileSystemModel implements TreeModel {

    private File root;
    private Vector listeners = new Vector();

    public FileSystemModel(File rootDirectory) {
        root = rootDirectory;
    }

    @Override
    public Object getRoot() {
        return root;
    }

    @Override
    public Object getChild(Object parent, int index) {
        File directory = (File) parent;
        String[] children = directory.list();
        /*       for (int j = 0; j< children.length; j++ ){
         System.out.println(children[j]);
         }       */
        return new FileSystemModel.TreeFile(directory, children[index]);
    }

    @Override
    public int getChildCount(Object parent) {
        File file = (File) parent;
        if (file.isDirectory()) {
            String[] fileList = file.list();
            if (fileList != null) {
                return file.list().length;
            }
        }
        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        File file = (File) node;
        return file.isFile();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        File directory = (File) parent;
        File file = (File) child;
        String[] children = directory.list();
        for (int i = 0; i < children.length; i++) {
            if (file.getName().equals(children[i])) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void valueForPathChanged(TreePath path, Object value) {
        File oldFile = (File) path.getLastPathComponent();
        String fileParentPath = oldFile.getParent();
        String newFileName = (String) value;
        File targetFile = new File(fileParentPath, newFileName);
        oldFile.renameTo(targetFile);
        File parent = new File(fileParentPath);
        int[] changedChildrenIndices = {getIndexOfChild(parent, targetFile)};
        Object[] changedChildren = {targetFile};
        fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);
    }

    private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
        TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
        Iterator iterator = listeners.iterator();
        TreeModelListener listener = null;
        while (iterator.hasNext()) {

            listener = (TreeModelListener) iterator.next();
            listener.treeNodesChanged(event);
        }
    }

    @Override
    public void addTreeModelListener(TreeModelListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeTreeModelListener(TreeModelListener listener) {
        listeners.remove(listener);
    }

    private class TreeFile extends File {

        public TreeFile(File parent, String child) {
            super(parent, child);
        }

        @Override
        public String toString() {
            return getName();
        }
    }
}

I'm sorry but I don't have that kind of time. It's your code, and you have to write it. If you get stuck on specific problems, then feel free to come back here for help.

this class (FileSystemModel), I took it from internet because it was my first time to use JTree. so don't think that am good in Java. the problem I have is where I can put those FTP queries. am so sorry to ask you many questions.

In that case, rather than rely on someone else's code that you don't understand, you will benefit more by doing it yourself.
Start by studying Oracle's excellent JTree tutorial, especially the section on creating a data model.
Then think about how the files and directories of your file system will correspond to the different elements and methods in the tree data model. Finally, think about how to get that info in a convenient form via FTP.
Just take it one small step at a time.

Edited 1 Year Ago by JamesCherrill

now I get some logic on how I can create tree data model. so the problem I have is this, I have only 2 days to finish this project. that's why am not able to read carefully all these theories. so can't you give me codes that I can refer from, if you have them. it will be helpful to me.

2 days is your problem, nobody else's. You are clearly expected to do your own homework, so don't expect anyone to do it for you.
As I said before, if you hit a specific problem, feel free to come back here for specific help.

This article has been dead for over six months. Start a new discussion instead.