JTree Question

For my application I want t bild a JTree that list the files in the directory the the file open.

I would like it to be able to perform simpole operations on them- e.g. Rename, delete

I would like the directories to expand like in a normal tree also...

Is this possible? If so, how would I go about doing it

Thanks in Advance- Alex

[361 byte] By [Glynndera] at [2007-11-26 13:54:03]
# 1
Look here: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=606454
Rodney_McKaya at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks, just wondering, how would i delete / rename files using that method?
Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 3
Add 2 menu item2 inside buildPopup for delete and rename and handle the action when they are pressed.Delete file:new File(fileName).delete();Rename is more tricky since you will have to edit the tree.
Rodney_McKaya at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 4
OK, thanks
Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 5
How do you remove a node again? (i forgot)
Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 6
> How do you remove a node again? (i forgot) http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
sabre150a at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

That doesnt help, it keeps getting a red line.

I must be putting it in the wrong place or something

item = new JMenuItem("Delete File") ;

item.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

deleteFile() ;

}

}) ;

and under delete file is the remove node thiing

Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 8
bump
Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 9
What is a red line and what is a remove node thing?
zadoka at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 10
I can't say anything to your remove-node problem but you may have a look at this:https://jdic.dev.java.net/#demosThere is some kind of fileViewer.Annette
Annettea at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 11

Red line in eclipse saying its incorrect or smething, what I want to do is have a delete file function(in my JTree) which I've built, however it doesnt remove the node when the file's deleted, it just stays there and does nothing, I'd like the node to dissappear

EDIT: Thanks for that link, I probaboly wont be using the file browser code, however the rest *should* be helpful

Message was edited by:

Glynnder

Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 12

> Red line in eclipse saying its incorrect or smething,

> what I want to do is have a delete file function(in

> my JTree) which I've built, however it doesnt remove

> the node when the file's deleted, it just stays there

> and does nothing, I'd like the node to dissappear

When you say red line and thing. I am having to guess what the real error is and real code is.

Please hold your mouse over the "red-line" to see what the error is. Or compile it by hand.

Also, it would be much more useful for you to post the code instead of saying "thing".

zadoka at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 13

I posted the code above, I'll do it again though.

item = new JMenuItem("Delete File") ;

item.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

deleteFile() ;

}

}) ;

popup.add(item) ;

If it helps its a right click menu

The error is because I dont know what code to use...

Glynndera at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 14

> I posted the code above, I'll do it again though.

You did but then you said: "and under delete file is the remove node thiing"

I am assuming "remove node thing" means code. Where is this code? What does the deleteFile() do?

> The error is because I dont know what code to use...

I thought you said Eclipse was telling you there was a compile error by underlining code in red. Is that true or not?

I would like to help but you are not being very clear.

http://catb.org/esr/faqs/smart-questions.html

zadoka at 2007-7-8 1:32:26 > top of Java-index,Desktop,Core GUI APIs...
# 15

OK, I'll be as clear as possible:

In my project I have a JTree that lists file in a directory, if the user right clicks a menu pops up, one of the options is "delete". This is the code for the delete option:

item = new JMenuItem("Delete File") ;

item.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

deleteFile() ;

}

}) ;

popup.add(item) ;

This is the code for deleteFile()

public void deleteFile(){

DefaultMutableTreeNode node = (DefaultMutableTreeNode)

tree.getLastSelectedPathComponent();

final String filepath = getSelectedFilepath(node) ;

System.out.println("Deleting: "+filepath);

new File(filepath).delete();

Main.log.append("Deleted!\n" + filepath);

}

What is happening is that, although the file deletes, the item in the JTree doesn't dissappear, I am wondering what the code is to make the node that the file it refers to has been deleted dissappear also.

By the remove node thing was the code I had tried:

tree.removeCurrentNode();

however that was underlined in red by Eclipse and wouldnt compile.

What is the correct code to remove the node?

Glynndera at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...
# 16

> By the remove node thing was the code I had tried:

> tree.removeCurrentNode();

> however that was underlined in red by Eclipse and

> wouldnt compile.

>

> What is the correct code to remove the node?

There is no method on the JTree called remvoeCurrentNode()

The API is very useful. It would be to your advantage to learn how to navigate it quickly (it will save you time in the future).

Also the sun tutorial is very useful and some information about changing a tree in a section here:

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

You are going to need to remove the node out of the tree, probably by calling a method on a MutableTreeNode. See here:

http://java.sun.com/javase/6/docs/api/javax/swing/tree/MutableTreeNode.html

zadoka at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...
# 17
I've looked at that and have no idea how to implement it....
Glynndera at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...
# 18

> I've looked at that and have no idea how to implement

> it....

Did you look at the MutableTreeNode link I posted? There were some remove methods.

Edit:

Also, another possible option is to reload the entire tree instead of trying to change the tree each time.

In most cases this is going to require a lot less code. It will also allow the tree to display the most current information.

zadoka at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...
# 19
yeah, how woul i do that?(or would i just do the code for it again)
Glynndera at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...
# 20

> yeah, how woul i do that?

> (or would i just do the code for it again)

It not a good idea to repeat code. Instead create a method or class that will build the tree for you.

After you add, delete files, etc. call this method to rebuild the tree. Rather than creating an entire JTree component. You should just be able to create a new data model:

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#data

zadoka at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...
# 21
OK thanks, I've done that 5 dukes to you, 5 dukes to the other guy
Glynndera at 2007-7-21 15:57:16 > top of Java-index,Desktop,Core GUI APIs...