Solved

Rename treenode when editing finishes

Posted on 2004-11-02
774 Views
Last Modified: 2011-09-20
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
Question by:cathalmchale
    19 Comments
     
    LVL 37

    Expert Comment

    by:zzynx
    >>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
     
    LVL 6

    Expert Comment

    by:expertmb
    the value is chnaged when editing stopped , you will get the new edited value.
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
     
    LVL 37

    Expert Comment

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

    >> How do I do the renaming bit here?????
    You don't have to
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> System.out.println( e.getClass().getName() );
    petmagdy,
    won't that just give

         "javax.swing.event.ChangeEvent"
    ?
    0
     
    LVL 6

    Expert Comment

    by:expertmb
    >>How do I do the renaming bit here?????
    renaming is done before this event gets fired.
    0
     
    LVL 6

    Expert Comment

    by:expertmb
    this event is fired when the user stops editing. i.e., he has already renamed the node.
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    ...hence:
    >>>> How do I do the renaming bit here?????
    >>You don't have to
    0
     

    Author Comment

    by:cathalmchale
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    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
     
    LVL 37

    Accepted Solution

    by:
    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
     
    LVL 92

    Expert Comment

    by:objects
    If you are interested in changes to the tree data, then use a TreeModelListener on your tree model.
    0
     

    Author Comment

    by:cathalmchale
    perfect, thanks!
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    Thank you
    :)
    0
     
    LVL 92

    Expert Comment

    by:objects
    you would be better off doing that using a TreeModelListener.
    0
     

    Author Comment

    by:cathalmchale
    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
     
    LVL 92

    Expert Comment

    by:objects
    CellEditorListeners are not really intended for that purpose, and could result in inconsistencies in your data.

    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> 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
     

    Author Comment

    by:cathalmchale
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now