[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 786
  • Last Modified:

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??
************/
      }
0
cathalmchale
Asked:
cathalmchale
  • 8
  • 4
  • 3
  • +2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now