home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SampleTree / SampleTreeModel.java < prev   
Encoding:
Java Source  |  1998-12-01  |  1.8 KB  |  59 lines

  1. /*
  2.  * @(#)SampleTreeModel.java    1.5 98/08/26
  3.  *
  4.  * Copyright 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.tree.DefaultTreeModel;
  16. import javax.swing.tree.TreeNode;
  17. import javax.swing.tree.TreePath;
  18. import javax.swing.tree.DefaultMutableTreeNode;
  19. import java.awt.Color;
  20.  
  21. /**
  22.   * SampleTreeModel extends JTreeModel to extends valueForPathChanged.
  23.   * This method is called as a result of the user editing a value in
  24.   * the tree.  If you allow editing in your tree, are using TreeNodes
  25.   * and the user object of the TreeNodes is not a String, then you're going
  26.   * to have to subclass JTreeModel as this example does.
  27.   *
  28.   * @version 1.5 08/26/98
  29.   * @author Scott Violet
  30.   */
  31.  
  32. public class SampleTreeModel extends DefaultTreeModel
  33. {
  34.     /**
  35.       * Creates a new instance of SampleTreeModel with newRoot set
  36.       * to the root of this model.
  37.       */
  38.     public SampleTreeModel(TreeNode newRoot) {
  39.     super(newRoot);
  40.     }
  41.  
  42.     /**
  43.       * Subclassed to message setString() to the changed path item.
  44.       */
  45.     public void valueForPathChanged(TreePath path, Object newValue) {
  46.     /* Update the user object. */
  47.     DefaultMutableTreeNode      aNode = (DefaultMutableTreeNode)path.getLastPathComponent();
  48.     SampleData    sampleData = (SampleData)aNode.getUserObject();
  49.  
  50.     sampleData.setString((String)newValue);
  51.     /* UUUhhhhh, pretty colors. */
  52.     sampleData.setColor(Color.green);
  53.  
  54.     /* Since we've changed how the data is to be displayed, message
  55.        nodeChanged. */
  56.     nodeChanged(aNode);
  57.     }
  58. }
  59.