Link to home
Start Free TrialLog in
Avatar of cathalmchale
cathalmchale

asked on

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??
************/
      }
Avatar of zzynx
zzynx
Flag of Belgium image

>>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()
?
Avatar of expertmb
expertmb

the value is chnaged when editing stopped , you will get the new edited value.
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

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

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

     "javax.swing.event.ChangeEvent"
?
>>How do I do the renaming bit here?????
renaming is done before this event gets fired.
this event is fired when the user stops editing. i.e., he has already renamed the node.
...hence:
>>>> How do I do the renaming bit here?????
>>You don't have to
Avatar of cathalmchale

ASKER

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 "";
  }
}
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?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are interested in changes to the tree data, then use a TreeModelListener on your tree model.
perfect, thanks!
Thank you
:)
you would be better off doing that using a TreeModelListener.
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??
CellEditorListeners are not really intended for that purpose, and could result in inconsistencies in your data.

>> 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...
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?