• Advertisement
Sign in to follow this  

[java] How would I implement this?

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement