File hierarchy Jtree problem! Please help!

Hi! I need some help :).

I have 2 Vectors.

The first is vDirs and that vector contains names of dirs, like this: 1|New Folder;2|New Folder 2; ... The number before the dir is the index of the dir, !not the index of the vector.

The second vector is vFiles amd it contains names of files, like this: 1|file.jpg; 1|file2.gif; 2|file3.jpg;... The number before the name of the file is telling in wich dir the file is located.

I want to place the content of the 2 vectors in one JTree. For example:

"file.jpg" & "file2.gif" are child tree nodes of "New Folder"...

I thing you got my point.

PLEASE HELP! :(

p.s. The vector are fill with dirs and files from a CD/DVD. The vectors' sizes are big :)

[751 byte] By [Dim3a] at [2007-10-2 20:15:16]
# 1

YOU DON'T HAVE TO WRITE IN CAPS, and there also is no need to put things as "Pease help!" in you subject; by posting in a forum, one usually has a question and needs help in order to get the question answered.

I think you'll get better responses if you post your question in a relevant part of this forum. A JTree is a class from the javax.swing package, so I suggest posting your question somewhere in the GUI Building - Swing part of this forum: http://forum.java.sun.com/forum.jspa?forumID=57

Good luck.

prometheuzza at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 2

Use:

- StringTokenizer class or split() method from String class to

parse dirs and files name

- Vector<String> class to keep String data

- HashMap class to keep key/Element values

- DefaultMutableTreeNode class to create an inner structure of

JTree

This is an example:

Vector<String> vDdir = new Vector<String>();

Vector<String> vFile = new Vector<String>();

public void initVectors(){

vDdir.add("1|New Folder");

vDdir.add("2|New Folder 2");

vFile.add("1|file.jpg");

vFile.add("1|file2.gif");

vFile.add("2|file3.jpg");

}

public String[] splitString(String string) throws Exception{

String id = "";

String data = "";

StringTokenizer token = new StringTokenizer(string, "|");

if (token.hasMoreTokens()){

id = token.nextToken();

}else{

throw new Exception("Illegal Format: \"" + string + "\"");

}

if (token.hasMoreTokens()){

data = token.nextToken();

}else{

throw new Exception("Illegal Format: \"" + string + "\"");

}

return new String[] { id, data};

}

public JTree initJTree(Vector<String> vDdir, Vector<String> vFile) throws Exception{

MutableTreeNode root = new DefaultMutableTreeNode();

HashMap<String, List><String>> fileMap = new HashMap<String, List><String>> ();

for(String file: vFile){

String[] splitFile = splitString(file);

String fileID= splitFile[0];

String fileName = splitFile[1];

List<String> fileList = fileMap.get(fileID);

if(fileList == null){

fileList = new LinkedList<String>();

fileMap.put(fileID, fileList);

}

fileList.add(fileName);

}

for(String dir: vDdir){

String[] splitDir = splitString(dir);

String dirID= splitDir[0];

String dirName = splitDir[1];

MutableTreeNode dirNode = new DefaultMutableTreeNode(dirName);

root.insert(dirNode, root.getChildCount());

for(String fileName: fileMap.get(dirID)){

MutableTreeNode fileNode = new DefaultMutableTreeNode(fileName);

dirNode.insert(fileNode, dirNode.getChildCount());

}

}

return new JTree( root );

}

raindropa at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 3
Thank you raindrop for the help, but i forgot to tell that there are sub-dirs. For example New Folder 4 is located in New Folder 3... Can you, or someone else post the solution of that? :)
Dim3a at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 4
You have also forgotten to say about relationships between dirs."1|New Folder" format contains only id and name of dir.There is no information about parent of the dir.
raindropa at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 5

In any way the solution for dirs and subdirs is the same

as above solution beetween dirs and subfiles.

You should know information about name of dir, id and id of parent dir.

After that you could put all dirs into HashMap, and when for generating the

tree structure for dirs, you could get subdirs for every dir from the

HashMap and add them into the JTree.

raindropa at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 6
Oh yes. The sub dirs are in this format:6|New Folder1/New folder7; 7|New Folder1/New Folder7/NewFolder18...The ID is for New Folder 7, so the files from vFiles with that ID are located in New Folder 7, and the New Folder 7 is located in New Folder1...
Dim3a at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 7

> Oh yes. The sub dirs are in this format:

> 6|New Folder1/New folder7; 7|New Folder1/New

> Folder7/NewFolder18...

>

> The ID is for New Folder 7, so the files from vFiles

> with that ID are located in New Folder 7, and the New

> Folder 7 is located in New Folder1...

The format "dir Id|parent dir Id|dir name" is better.

Transform your format to this and adapt the previous algorithm

( dirs - sub files ) to the necessary ( dir - subdirs )

raindropa at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 8
Hi Dim3 , if you have some troubles with the proposed algorithm,do not hesitate to ask question about it.
raindropa at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...
# 9

Yes i'm trying to do something but i can't do anything. And I cant change the format to "ID|dir|bub-dir" because i must change another alghoritham in my program ant that is a lot of work :). Can you help me with this format "id|NewFolder\NewFolder\NewFolder".The file hierarchy is from a CD or DVD. So there a lot of files dir and sub dirs.

Dim3a at 2007-7-13 22:57:32 > top of Java-index,Other Topics,Algorithms...