Solved

removeChild method not removing the children from the tree

Posted on 2009-05-05
5
223 Views
Last Modified: 2012-05-06
Hello Experts,

I'm having problems removing child nodes from a JTreeTable.  Can someone tell me what I'm doing wrong?  

Thanks in advance.


private void traverse (Row row)
   {
      // If we are at the deepest parent level and its children have been loaded then
      // we need to sort the children.
      if ( (row.getLevel () == m_nLevels - 1) && (row.isFullyLoaded ()) )
      {
         ArrayList<Row> list = Collections.list (row.children ());
         for (Row r : list)
            System.out.println ("Row : " + (String) r.getData ().getValue (m_nSortColumn));
            
         Collections.sort (list, this);
         System.out.println ("Sorted ------------------------------");
         for (Row r : list)
            System.out.println ("Row : " + (String) r.getData ().getValue (m_nSortColumn));
         
         for (int i = list.size () - 1; i >= 0; i--)
            removeChild (row, i);
         
         for (int i = 0; i < list.size (); i++)
            appendChild (row, list.get (i).getData ());
      }
      else
      {
         for (int r = 0; r < row.getChildCount (); r++)
         {
            Row child = row.getChild (r);
            traverse (child);
         }
      }
   }
 
 
 
 public void removeChild (Row parent, int index)
   {
      assert (parent != null);
 
      parent.remove (index);
 
      // When this model is first being created we do not have a reference to the
      // tree component because our reference only gets set after we construct the
      // model and tell the tree about it. This is a circular dependency but we do
      // not need to have it then because we only need to notify the component
      // about rows added after the initialization.
      if (m_Tree != null)
         fireModelEvent (NODE_DELETED, buildPath (parent), index, null);
   }

Open in new window

0
Comment
Question by:brohjoe
[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
  • 3
  • 2
5 Comments
 

Author Comment

by:brohjoe
ID: 24311253
I think I should explain a little more and add some points since it seems no one has stepped to the plate yet.  What I'm trying to do is delete the child nodes of this JTreeTable during a sort function which is called after the user clicks on the column header, then append the new children to it.  The append function works perfectly, but the 'removeChild()' function is not deleting the children.  I can't figure out what's wrong.   I'm willing to entertain any reasonable comment here.  
0
 
LVL 92

Expert Comment

by:objects
ID: 24311441
check the event that is getting fired, thats the most common cause of problems

0
 

Author Comment

by:brohjoe
ID: 24314733
//This is the 'event' in question:

for (int i = list.size () - 1; i >= 0; i--)
            removeChild (row, i);


//This method calls:
 public void removeChild (Row parent, int index)
   {
      assert (parent != null);

      parent.remove (index);
   
//which calls:
public void remove(int childIndex) {
      MutableTreeNode child = (MutableTreeNode)getChildAt(childIndex);
      children.removeElementAt(childIndex);
      child.setParent(null);
    }

//if we step over the last function we get::
 if (m_Tree != null)
         fireModelEvent (NODE_DELETED, buildPath (parent), index, null);

//if we step into that 'child.setParent(null) function we get::
 public TreeNode getChildAt(int index) {
      if (children == null) {
          throw new ArrayIndexOutOfBoundsException("node has no children");
      }
      return (TreeNode)children.elementAt(index);
    }

which calls a Vector class:

public synchronized void removeElementAt(int index) {
      modCount++;
      if (index >= elementCount) {
          throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                         elementCount);
      }
      else if (index < 0) {
          throw new ArrayIndexOutOfBoundsException(index);
      }
      int j = elementCount - index - 1;
      if (j > 0) {
          System.arraycopy(elementData, index + 1, elementData, index, j);
      }
      elementCount--;
      elementData[elementCount] = null; /* to let gc do its work */
    }

Everything seems to be in order, it compiles and I get no error messages.

<Points increased by 100>
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24321325
need to see the code for fireModelEvent() and buildPath()
(any reason why you don't use DefaultTableModel)

0
 

Author Closing Comment

by:brohjoe
ID: 31578198
Thanks objects for your answer.  'fireModelEvent' is exactly where the problem was.   Appreciate ya!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 23
servlet example 17 59
Selenium docs api java index 3 65
junit example issue 2 25
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 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