Another JTree question

What is the code to make a non-dynamic JMenu editable (on slow double click/right click) and then see what the node has been edited to and have the new name as a string
[175 byte] By [Glynndera] at [2007-11-26 13:59:24]
# 1
Maybe this will help you: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=561608
Rodney_McKaya at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 2
it doesnt really- as I dont know how to take the string they put in, when they pressed enter (so what it renamed to), and then use it to rename the actual file if you get what i mean
Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 3
> if you get what i meanNot really. What are you asking then? Are you trying to make a JMenu editable or a JTree, you are confusing me?
zadoka at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorry, i had the worng word d'oh!Im trying to make a JTree editable, and whatever they set the name to it renames the actual fileto that.The link you posted actually does help a lot, i didnt see the second example
Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hmm, I tried integrating the second example into my code and I had this error whe I tried to rename a file:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to base.FileExplorer$NodeObject

at base.FileExplorer$EditController$1.actionPerformed(FileExplorer.java:172)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 6
Looks like you are casting a String at line 172 of FileExplorer
zadoka at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 7
hwat do you mean by that?and what should it be?
Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 8

> hwat do you mean by that?

> and what should it be?

Well if you read the exception it tell you what line it is on. That is what I listed. The stack track you posted also told you what the problem is. You are casting to something on that line to a String, but the problem is that the object is not a String.

zadoka at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 9
OK, thanks I see what you mean.
Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 10

You're mixing your code with the example.

You should not cast node.getUserObject() to NodeObject, because it looks like your user object is a String.

Try this instead:

DefaultMutableTreeNode node =

(DefaultMutableTreeNode)path.getLastPathComponent();

node.setUserObject(tf.getText();

DefaultTreeModel model = (DefaultTreeModel)tree.getModel();

model.nodeChanged(node);

Rodney_McKaya at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 11
wow, brilliant thanks!With a bit more modification i'll be perfect ;)I'll give you 7 dukes cos im sure ill have more issues :p
Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 12

Yeah, I do.

How would you take that and rename that file to br called "tf.getText"

EDIT: OK, i have got the renaming bit sorted, just one last thing, how would you get the file related to that node?

FINAL EDIT:

ok, using this code:

DefaultMutableTreeNode nodenode = (DefaultMutableTreeNode)

tree.getLastSelectedPathComponent();

final String filepath = getSelectedFilepath(nodenode) ;

System.out.println("Renaming: "+filepath+" to "+tf.getText());

new File(filepath).renameTo(new File(tf.getText()));

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

TreePath path = tree.getSelectionPath();

DefaultMutableTreeNode node =

(DefaultMutableTreeNode)path.getLastPathComponent();

node.setUserObject(tf.getText());

DefaultTreeModel model = (DefaultTreeModel)tree.getModel();

model.nodeChanged(node);

i got it to rename, but the issue is that it renames it AND moves it to the current directory (which is "G:\workspace\LDK\"), so, is there a way for it to keep it in its same directory?

Message was edited by:

Glynnder

Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 13
Build the full path for renaming:renameTo(new File(new File(filepath).getParent() + "/" + tf.getText());
Rodney_McKaya at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...
# 14
Great thanks!worksthe final dukes are yours ;)
Glynndera at 2007-7-8 1:40:12 > top of Java-index,Desktop,Core GUI APIs...