Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename treenode when editing finishes

Posted on 2004-11-02
19
Medium Priority
?
781 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
Comment
Question by:cathalmchale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +2
19 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12472788
>>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
ID: 12472806
the value is chnaged when editing stopped , you will get the new edited value.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12472847
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 12472848
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
ID: 12472860
>> System.out.println( e.getClass().getName() );
petmagdy,
won't that just give

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Author Comment

by:cathalmchale
ID: 12473009
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
ID: 12473198
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:
zzynx earned 2000 total points
ID: 12473305
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
ID: 12478739
If you are interested in changes to the tree data, then use a TreeModelListener on your tree model.
0
 

Author Comment

by:cathalmchale
ID: 12480849
perfect, thanks!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12480881
Thank you
:)
0
 
LVL 92

Expert Comment

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

Author Comment

by:cathalmchale
ID: 12480957
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
ID: 12480976
CellEditorListeners are not really intended for that purpose, and could result in inconsistencies in your data.

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12481076
>> 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
ID: 12481094
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

597 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