Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4199472

DefaultTreeModel.nodeChanged() has no effect when applied to the root of a JTree

    XMLWordPrintable

Details

    • 1.2.2
    • x86, sparc
    • solaris_2.6, windows_nt

    Description



      Name: dbT83986 Date: 12/22/98


      1. Compile and execute the example below. Clicking
      on the top button changes the value of the root
      node and calls nodeChanged(). However, the root
      node is not repainted.

      The bottom button shows that the same code applied
      to the child node works correctly and repaints the
      child node with the new value.

      2.

      import com.sun.java.swing.*;
      import com.sun.java.swing.tree.*;
      import java.awt.BorderLayout;

      public class TreeNodeBug extends Object {

        public static void main(String[] argv) {
          JFrame frame = new JFrame();
          frame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent event) {
              event.getWindow().dispose();
              System.exit(0);
            }
          });
          frame.setSize(450, 300);
          frame.getContentPane().setLayout(new BorderLayout());
          frame.show();

          // Create 3 nodes
          final DefaultMutableTreeNode root = new DefaultMutableTreeNode("root", true);
          final DefaultMutableTreeNode child = new DefaultMutableTreeNode("child", true);
          final DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("leaf", false);
          root.add(child);
          child.add(leaf);

          // Create the tree and add it to the frame
          final JTree tree = new JTree(root);
          frame.getContentPane().add(tree, BorderLayout.CENTER);

          // Create a button that changes the root node
          JButton button1 = new JButton("Change the \"root\" node");
          button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent event) {
              root.setUserObject("New root");
              ((DefaultTreeModel)tree.getModel()).nodeChanged(root);
            }
          });
          frame.getContentPane().add(button1, BorderLayout.NORTH);

          // Create a button that changes the child node
          JButton button2 = new JButton("Change the \"child\" node");
          button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent event) {
              child.setUserObject("New child");
              ((DefaultTreeModel)tree.getModel()).nodeChanged(child);
            }
          });
          frame.getContentPane().add(button2, BorderLayout.SOUTH);

          frame.validate();
        }
      }

      3. N/A

      4. N/A

      5. N/A
      (Review ID: 38796)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              svioletsunw Scott Violet (Inactive)
              dblairsunw Dave Blair (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: