[java] How would I implement this?

Started by
6 comments, last by The C modest god 17 years, 9 months ago
I wish to have a hirarchy of directories. In each directory I may have zero or more sub directories, and zero or more strings. Should I use the Tree class? Or should I create my own composite design pattern class? Also, how would I deal with the GUI of this? I can create a dialog in net beans that will contain some buttons. However, I want to present these directories and strings, much like windows explorer presents its file system. That means I need to be able to close and open each directory and to select directories and files, so when I will press one of the buttons of the dialog, it will operate on the last item I selected. Can you help me with this problem? Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
Hi.

This is simply my 2 cents. There might be better alternatives out there.

I would use a JTree component as a view (gui). Before initialising it, I would create a TreeModel with which I fill my information.

In case you wonder about how to traverse the directories as well, you should use the File class. Choose a "root" directory, and recursively call File's listFiles() method to traverse through the directories. ( File has a method isDirectory() which you can use to differ between files and directories.) The File class also has methods that lets you check your program's access rights to the files/directories.

Remember that initialising a File object (successfully) does not mean that the file or directory actually exists. Check that with the exists() method.

The API for the File object is pretty self-explanatory.

For more information on how to use Tree structures, take a look at
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html


Good luck ;)
Development blog, The Omega Sector -- http://www.omegasector.org
When I said directories I didnt mean to read the directories of the hard drive!
Its just the way I will be presenting and storing some of my data in the ram.

I have created a dialog with netbeans, and used the JTree class in it.
I copied the code into eclipse.
Now it presents me a tree with all sort of directories such as food and stuff like that.
Is that the default tree?

I have already read the "how to" you have posted, but I havnt found there the following:
How do I remove directories from a tree. Both single and the whole tree directories structure.
And how do I edit tree "leaves"?
I couldnt find methods that will do that in JTree.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
You could keep a list (Vector or ArrayList) of objects. If an object is a list, then that object itself is another list of objects or Lists. Etc. You could also use a Hashtable or something if you want to name the directories.

When you want to display this structure, you can use a tree.

EDIT: Instead of Objects or Lists, you would use Strings or Lists as specified in your original post.
How would I use the JTree to display it?
I am unable to remove items from it.
Should I recreate the JTree each time I update the hirerachy of lists and strings?
How do I create a new JTree with the lists I want?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Adding nodes to JTree

http://javaalmanac.com/egs/javax.swing.tree/AddNode.html

at the bottom, there is also a link to an example of removing nodes from a JTree, and other JTree related stuff.

Hope you find what you're looking for.
Development blog, The Omega Sector -- http://www.omegasector.org
I have added a DefaultMutableTreeNode to my tree.
However, I want it to be a directory but the Icon it shows is not of a directory.
How do I make it show a directory icon?

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Please help with this.
I want to edit the name of the JTree node. I did something like this:
		    		DefaultMutableTreeNode node = (DefaultMutableTreeNode)mPath.getLastPathComponent();		    		// TODO: Check if no bug over here		    		node.setUserObject(txtName.getText());

Will this create bugs?
Also when I do this, the name is updated in the GUI only after I select something else.
Please help.
Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement