Rename treenode when editing finishes

public void editingStopped(ChangeEvent e)
      {
        DefaultMutableTreeNode = (DefaultMutableTreeNode ) tree.getSelectionPath().getLastPathComponent();
        treeModel.nodeStructureChanged(node);
       
      /********
How do I do the renaming bit here?????
The user was editing the node, changed the text and this event was fired - how do i get the new text??
************/
      }
cathalmchaleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
>>how do i get the new text??

Well, since you have the TreeNode it can't be that difficult.
What about  
        node.toString()

or

        node.getUserObject().toString()
?
0
expertmbCommented:
the value is chnaged when editing stopped , you will get the new edited value.
0
petmagdyCommented:
Please first Post this result:

System.out.println( e.getClass().getName() );

This will answer ur question and then if we got the text from this object we can set the node text

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

zzynxSoftware engineerCommented:
Oh, (now I think) I see what you mean...

>> How do I do the renaming bit here?????
You don't have to
0
zzynxSoftware engineerCommented:
>> System.out.println( e.getClass().getName() );
petmagdy,
won't that just give

     "javax.swing.event.ChangeEvent"
?
0
expertmbCommented:
>>How do I do the renaming bit here?????
renaming is done before this event gets fired.
0
expertmbCommented:
this event is fired when the user stops editing. i.e., he has already renamed the node.
0
zzynxSoftware engineerCommented:
...hence:
>>>> How do I do the renaming bit here?????
>>You don't have to
0
cathalmchaleAuthor Commented:
hi, ill just post all the code - it is just a Default cell editor, i didnt bother to over-ride anything.  And ill post the tree node class too.  The idea is when the user edits the node, the associated directory will be renamed.  Thankyou.
/*********  THE TREE EDITOR DECLARATION **************/
CellEditorListener cel = new CellEditorListener()
    {
      public void editingStopped(ChangeEvent e)
      {
        FileTreeNode node = (FileTreeNode) tree.getSelectionPath().getLastPathComponent();
        treeModel.nodeStructureChanged(node);
        String s = e.toString();
        // physically rename directory
        //renameDirectory(node.getFile())
      }


      public void editingCanceled(ChangeEvent e)
      {
      }
    };
    DefaultTreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer);
    editor.addCellEditorListener(cel);
    tree.setCellEditor(editor);
    tree.setEditable(true);

/*************** TREE NODE CLASS ************************/
public class FileTreeNode extends DefaultMutableTreeNode
{
  /** FileTreeNode - File userObject, boolean allowsChildren */
  public FileTreeNode(File userObject, boolean allowsChildren)
  {
    super(userObject, true);
  }


  /** FileTreeNode - File userObject, allows children (by default) */
  public FileTreeNode(File userObject)
  {
    super(userObject, true);
  }


  /** FileTreeNode - NO userObject, allows children (by default) */
  public FileTreeNode()
  {
    super();
  }


  /** Convenience method - set the userObject, only type java.io.File allowed */
  public void setFile(File userObject)
  {
    this.setUserObject(userObject);
  }


  /** Convenience method - get the userObject, generic type java.io.File allowed */
  public File getFile()
  {
    return (File) this.getUserObject();
  }


  /** Over-ride super-class, only sets userObject if of type java.io.File */
  public void setUserObject(Object userObject)
  {
    if (userObject instanceof File)
      super.setUserObject(userObject);
  }
 
 
  /** Returns the filename of the java.io.File userObject */
  public String toString()
  {
    File temp = getFile();
    if(temp != null)
      return temp.getName();
    else
      return "";
  }
}
0
zzynxSoftware engineerCommented:
I see.
What do you see happening with the code you have posted when editing stops?
The tree node text changed while the directory name isn't?
0
zzynxSoftware engineerCommented:
Could you try (not sure at all):

CellEditorListener cel = new CellEditorListener()  {
      public void editingStopped(ChangeEvent e)
      {
         FileTreeNode node = (FileTreeNode) tree.getSelectionPath().getLastPathComponent();
         treeModel.nodeStructureChanged(node);

         DefaultTreeCellEditor editor = (DefaultTreeCellEditor)yourTree.getCellEditor();
         String newName = (String) editor.getCellEditorValue();
         System.out.println("new name= " + newName);
         // Do the renaming
         // ...
      }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
If you are interested in changes to the tree data, then use a TreeModelListener on your tree model.
0
cathalmchaleAuthor Commented:
perfect, thanks!
0
zzynxSoftware engineerCommented:
Thank you
:)
0
objectsCommented:
you would be better off doing that using a TreeModelListener.
0
cathalmchaleAuthor Commented:
If I may ask 1 more question?
I solved the problem with:

CellEditorListener cel = new CellEditorListener()
    {
      public void editingStopped(ChangeEvent e)
      {
        // fire change in model
        TreePath path = tree.getSelectionPath();
        FileTreeNode node = (FileTreeNode) path.getLastPathComponent();
        treeModel.nodeStructureChanged(node);
        // apply change to user object (File)
        DefaultTreeCellEditor editor = (DefaultTreeCellEditor)tree.getCellEditor();
        String newName = (String) editor.getCellEditorValue();
        if(newName != null)
        {
          File newFile = new File(node.getFile().getParent() + "\\" + newName);
          if (!newFile.exists())
          { // physically rename directory and update user object
            if (node.getFile().renameTo(newFile))
              node.setFile(newFile);
          }
          else
          {
            I18N.mkDialog(bundle, null, "error.fileExists", "error.user.dialogTitle", JOptionPane.ERROR_MESSAGE);
            // re-start editing
            tree.startEditingAtPath(path);
       
/************* I restart cell editing here like eg, Windows explorer would.  But am I able to select all the text of this node??  when tree editing starts nothing is selected and the cursor is in last position - i would just like all the text selected??
0
objectsCommented:
CellEditorListeners are not really intended for that purpose, and could result in inconsistencies in your data.

0
zzynxSoftware engineerCommented:
>> i would just like all the text selected
Only solution I see: calling getTreeCellEditorComponent() on editor which will give you the JTextField used for editing.
But calling that one is not that easy since it needs a whole bunch of parameters.
Not sure if you have all of them available...
0
cathalmchaleAuthor Commented:
So I should use a TreeModelListener.  I would still like to use a DefaultTreeModel?
So I inherit the methods:

  public void treeNodesChanged(TreeModelEvent e)
  {
  }
  public void treeNodesInserted(TreeModelEvent e)
  {
  }
  public void treeNodesRemoved(TreeModelEvent e)
  {
  }
  public void treeStructureChanged(TreeModelEvent e)
  {
  }

How now would I achieve the same as in the code I posted above?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.