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)
> 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.
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);
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