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

swing.tree.DefaultTreeModel.insertNodeInto throws ArrayIndexOutOfBoundsException

    • sparc
    • solaris_2.5.1



      Name: akC57697 Date: 02/19/99



           The swing.tree.DefaultTreeModel.insertNodeInto throws
       ArrayIndexOutOfBoundsException in JDK1.2.2G

       The javadoc says:
      "
      public void insertNodeInto(MutableTreeNode newChild,
                                 MutableTreeNode parent,
                                 int index)

            Invoked this to insert newChild at location index in parents
            children. This will then
            message nodesWereInserted to create the appropriate event. This
            is the preferred way to add children as it will create the
            appropriate event.

      "

      ---------------------------------------8-<-----------------------------------
      import java.util.Enumeration;
      import javax.swing.event.TreeModelListener;
      import javax.swing.event.TreeModelEvent;
      import javax.swing.tree.*;


      public class Test {

          /* create the TreeModel of deep 2 with specified num of children */
          DefaultMutableTreeNode child0 = null;

          public DefaultTreeModel getTreeModelILike2(int level1, int level2) {
              DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
              for (int i = 0; i < level1; i++) {
                  DefaultMutableTreeNode child1 = new
                          DefaultMutableTreeNode("root.child" + i);
                  for (int j = 0; j < level2; j++) {
                      DefaultMutableTreeNode child2 = new
                              DefaultMutableTreeNode("root.child" + i + ".child" + j);
                      child1.insert(child2, j);
                  }
                  root.insert(child1, i);
                  if (i == 0) {
                      child0 = child1;
                  }
              }
              return new DefaultTreeModel(root);
          }

          public static void main(String argv[]) {
              Test t = new Test();
              DefaultMutableTreeNode newchild = new
                      DefaultMutableTreeNode("root.newchild");
              DummyVHLC fhlc = new DummyVHLC();
              DefaultTreeModel model = t.getTreeModelILike2(3, 3);
              fhlc.setModel(model);
              model.addTreeModelListener(fhlc);
              fhlc.setRootVisible(true);
              TreePath path = fhlc.getPathForRow(1);
              Object object = new Integer(1999);
              TreePath root = fhlc.getPathForRow(0);
              Object o = root.getLastPathComponent();
              model.insertNodeInto(newchild, (MutableTreeNode) o, 3);
              TreeModelEvent e = fhlc.getLastEvent();
              if ((e != null) && ("treeNodesInserted".equals(fhlc.getLastMethod()))
                      && (e.getTreePath().equals(root))
                      && (e.getChildIndices().length == 1)
                      && (e.getChildIndices() [0] == 3)) {
                  System.out.println("Ok.");
                  System.exit(0);
              }
              System.out.println("TreeModelEvent has not been created properly.");
              System.exit(1);
          }
      }


      class DummyVHLC extends VariableHeightLayoutCache implements TreeModelListener {
          private String method = null;
          private TreeModelEvent event = null;

          public DummyVHLC() {
              super();
          }

          public void treeNodesChanged(TreeModelEvent e) {
              method = "treeNodesChanged";
              event = e;
              super.treeNodesChanged(e);
          }

          public void treeNodesInserted(TreeModelEvent e) {
              method = "treeNodesInserted";
              event = e;
              super.treeNodesChanged(e);
          }

          public void treeNodesRemoved(TreeModelEvent e) {
              method = "treeNodesRemoved";
              event = e;
              super.treeNodesChanged(e);
          }

          public String getLastMethod() {
              String s = method;
              method = null;
              return s;
          }

          public TreeModelEvent getLastEvent() {
              TreeModelEvent e = event;
              event = null;
              return e;
          }
      }
      ---------------------------------------8-<-----------------------------------
      java full version "JDK-1.2.2-E"
      java Test
      Ok.

      java full version "JDK-1.2.2-G"
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
      at java.util.Vector.elementAt(Vector.java:409)
      at javax.swing.tree.DefaultMutableTreeNode.getChildAt(DefaultMutableTreeNode.java:226)
      at javax.swing.tree.VariableHeightLayoutCache.treeNodesChanged(VariableHeightLayoutCache.java:364)
      at DummyVHLC.treeNodesInserted(Test.java:75)
      at javax.swing.tree.DefaultTreeModel.fireTreeNodesInserted(DefaultTreeModel.java:467)
      at javax.swing.tree.DefaultTreeModel.nodesWereInserted(DefaultTreeModel.java:290)
      at javax.swing.tree.DefaultTreeModel.insertNodeInto(DefaultTreeModel.java:219)
      at Test.main(Test.java:43)


      ======================================================================

            svioletsunw Scott Violet (Inactive)
            akuzminorcl Alexander Kuzmin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: