7 Replies Latest reply on Apr 18, 2007 9:54 PM by sunnycode

    How can I persist all changes I made to these edited leaves and nodes??

    sunnycode
      Dear all friends:
      sorry to bother,
      I have following code can edit the leaf of the JTree,
      But after i edit it, I found what I updated is lost, How can I persist all changes I made to these edited leaves and nodes(not leaf).
      ie, to keep all these changes until i update them later.
      Foe Example, I change node name from "blue" in Directory of Color to "NewHouse", then after I exit editing, node not return to blue but keep "NewHouse".

      thanks

      sunny
      import java.awt.BorderLayout;
      import java.util.EventObject;
      
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTree;
      import javax.swing.tree.DefaultTreeCellEditor;
      import javax.swing.tree.DefaultTreeCellRenderer;
      import javax.swing.tree.TreeCellEditor;
      import javax.swing.tree.TreeNode;
      
      public class EditLeafSample {
        public static void main(String args[]) {
          JFrame frame = new JFrame("Editable Tree");
          JTree tree = new JTree();
          tree.setEditable(true);
      
          DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
          TreeCellEditor editor = new LeafCellEditor(tree, renderer);
          tree.setCellEditor(editor);
      
          JScrollPane scrollPane = new JScrollPane(tree);
          frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
          frame.setSize(300, 150);
          frame.setVisible(true);
        }
      }
      
      class LeafCellEditor extends DefaultTreeCellEditor {
      
        public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
          super(tree, renderer);
        }
      
        public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer,
            TreeCellEditor editor) {
          super(tree, renderer, editor);
        }
      
        public boolean isCellEditable(EventObject event) {
          // Get initial setting
          boolean returnValue = super.isCellEditable(event);
          // If still possible, check if current tree node is a leaf
          if (returnValue) {
            Object node = tree.getLastSelectedPathComponent();
            if ((node != null) && (node instanceof TreeNode)) {
              TreeNode treeNode = (TreeNode) node;
              returnValue = treeNode.isLeaf();
            }
          }
          return returnValue;
        }
      }
        • 1. Re: How can I persist all changes I made to these edited leaves and nodes??
          843805
          Override the valueForPathChanged method of your TreeModel and do the following. You may also want to set the userObject of your TreeNode with the new value.

          Let me know if this helps...
          public void valueForPathChanged(TreePath path, Object newValue) 
          {
              super.valueForPathChanged(path, newValue);
              TreeNode aNode = (TreeNode)path.getLastPathComponent();
              ((MutableTreeNode)aNode).setUserObject(newValue);
              nodeChanged(aNode);
          }
          • 2. Re: How can I persist all changes I made to these edited leaves and nodes??
            843805
            I believe what you're after, is that when you change the text and click somewhere else so that focus is lost on the leaf, the text isn't reverted. You are, of course, aware that if you hit the return key, an action is fired, focus is lost and the new value is saved?
            • 3. Re: How can I persist all changes I made to these edited leaves and nodes??
              sunnycode
              Hi, Cantry, Thanks for your help.
              I add your code as follows:
              import java.awt.BorderLayout;
              import java.util.EventObject;
              
              import javax.swing.JFrame;
              import javax.swing.JScrollPane;
              import javax.swing.JTree;
              import javax.swing.JTree.*;
              import javax.swing.tree.*;
              import javax.swing.tree.DefaultTreeCellEditor;
              import javax.swing.tree.DefaultTreeCellRenderer;
              import javax.swing.tree.TreeCellEditor;
              import javax.swing.tree.TreeNode;
              
              public class EditLeafSample {
                public static void main(String args[]) {
                  JFrame frame = new JFrame("Editable Tree");
                  JTree tree = new JTree();
                  tree.setEditable(true);
              
                  DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
                  TreeCellEditor editor = new LeafCellEditor(tree, renderer);
                  tree.setCellEditor(editor);
              
                  JScrollPane scrollPane = new JScrollPane(tree);
                  frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
                  frame.setSize(300, 150);
                  frame.setVisible(true);
                }
              }
              
              class LeafCellEditor extends DefaultTreeCellEditor {
              
                public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
                  super(tree, renderer);
                }
              
                public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer,
                    TreeCellEditor editor) {
                  super(tree, renderer, editor);
                }
              
                public void valueForPathChanged(TreePath path, Object newValue) 
                {
                    super.valueForPathChanged(path, newValue);
                    TreeNode aNode = (TreeNode)path.getLastPathComponent();
                    ((MutableTreeNode)aNode).setUserObject(newValue);
                    nodeChanged(aNode);
                }
              
              
                public boolean isCellEditable(EventObject event) {
                  // Get initial setting
                  boolean returnValue = super.isCellEditable(event);
                  // If still possible, check if current tree node is a leaf
                  if (returnValue) {
                    Object node = tree.getLastSelectedPathComponent();
                    if ((node != null) && (node instanceof TreeNode)) {
                      TreeNode treeNode = (TreeNode) node;
                      returnValue = treeNode.isLeaf();
                    }
                  }
                  return returnValue;
                }
              }
              but following lines show red,

              super.valueForPathChanged(path, newValue);
              and
              nodeChanged(aNode);

              can you help more??
              Thanks
              sunny
              • 4. Re: How can I persist all changes I made to these edited leaves and nodes??
                843805
                That's a method for the TreeModel, not the CellEditor.
                • 5. Re: How can I persist all changes I made to these edited leaves and nodes??
                  sunnycode
                  Dear Sir, thanks,
                  I implements the interface TreeModel already, I have following code:
                    public void valueForPathChanged(TreePath path, Object newValue) 
                    {
                        valueForPathChanged(path, newValue);
                        TreeNode aNode = (TreeNode)path.getLastPathComponent();
                        ((MutableTreeNode)aNode).setUserObject(newValue);
                        nodeChanged(aNode);
                    }
                  but line nodeChanged still red, can you help tell where is this nodeChanged (aNode) available??
                  Thanks
                  sunny
                  • 6. Re: How can I persist all changes I made to these edited leaves and nodes??
                    843805
                    nodeChanged() is not part of the TreeModel interface. Instead of implementing the interface, try extending DefaultTreeModel. The method is in there.
                    • 7. Re: How can I persist all changes I made to these edited leaves and nodes??
                      sunnycode
                      thanks a lot!! yes, you are right, once extends that class, this method will be available.