Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JTree - Remove All Nodes

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,655
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default JTree - Remove All Nodes

    I just have a quick question.

    I have a JTree with X number of Nodes and X number of levels and I wanted to know how I would go about removing all of the nodes from the JTree, with the exception of the root node.

    Can anyone give me some tips?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,655
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: JTree - Remove All Nodes

    Ok, so I figured that out by looping through until the root is a leaf and removing nodes using the DefaultTreeModel.removeNodeFromParent() method.

    Now I have a new problem.

    The reason I wanted to remove all the nodes is because I want to remove all the nodes, add a bunch of new nodes to the root to recreate the tree a different way. The problem is that once I do that I cannot expand the root node anymore. The root does have the symbol that says it has children and I have checked to make sure the root has children.

    For whatever reason, the ability to expand the tree looks like it is gone. I have tried to expand programatically, but that also doesn't work.

    Any thoughts?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: JTree - Remove All Nodes

    Can you post a short example that demos the problem?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,655
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: JTree - Remove All Nodes

    Ok, well I have a method that builds the tree. I would think that all of this is essential:
    public void buildTree(Location o,Location d,int deptTime)
        {
        	DefaultTreeModel model = (DefaultTreeModel)connectTree.getModel();
        	Object root = model.getRoot();
        	while(!model.isLeaf(root))
        	{
        		model.removeNodeFromParent((MutableTreeNode)model.getChild(root,0));
        	}
     
        	Vector<Flight> heads = new Vector<Flight>();
        	for(int i=0;i<currentRoutes.size();i++)
        	{
        		if(currentRoutes.get(i).origin.isSame(o) && currentRoutes.get(i).dest.isSame(d))
        			heads.add(currentRoutes.get(i));
        	}
        	for(int i=0;i<heads.size();i++)
        	{
        		Flight head = heads.get(i);
        		int tempTime = head.arrTime+deptTime;
        		DefaultMutableTreeNode headNode = new DefaultMutableTreeNode(head.toString());
        		Vector<Flight> parts = new Vector<Flight>();
        		for(int x=0;x<currentRoutes.size();x++)
    	    	{
    	    		if(currentRoutes.get(x).origin.isSame(d) && (currentRoutes.get(x).deptTime>=tempTime-15 && currentRoutes.get(x).deptTime<=tempTime+15))
    	    			parts.add(currentRoutes.get(x));
    	    	}
    	    	top.add(headNode);
    	    	for(int x=0;x<parts.size();x++)
    	    	{
    	    		headNode.add(new DefaultMutableTreeNode(parts.get(x).toString()));
    	    	}
        	}
        }

    Basically, two Location Objects are sent to the method. The first is an origin and the second is a departure. These Location Objects are compared to a list of Flight Objects (which contain an Origin and Destination Location) to see if the Flight Object represents the route sent to the method. Multiple Flight Objects can represent the route, just on different times. Then an int is sent to the method (representing minutes) which is used to find a list of Flights that connect with the route sent to the method, and with a layover of the minutes sent to the method (and a buffer of 15 minutes before and after for approximation). Then, a tree node, whose parent is the root, is created for each Flight Object that flies the route sent to the method. And, any connections for that Flight Object are children. Some routes may not have connections, so they have no children.

    A Location Object contains a Name and a coordinate that represents its location on a map. Fairly simple.

    A Flight Object contains a bunch of information, but for this only the Origin Location, Destination Location, and Departure Time are used.


    Thats really the all the information for this issue. This method is triggered by a JButton that draws routes out on a map and then creates a JTree of the route connections represented in the map. The problem is the JTree isn't letting me expand the Root Node the second time this method is called and causes the following error the third time this method is called:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.tree.DefaultMutableTreeNode.getChildAt (DefaultMutableTreeNode.java:230)
    at javax.swing.tree.VariableHeightLayoutCache.treeNod esRemoved(VariableHeightLayoutCache.java:543)
    at javax.swing.plaf.basic.BasicTreeUI$Handler.treeNod esRemoved(BasicTreeUI.java:3810)
    at javax.swing.tree.DefaultTreeModel.fireTreeNodesRem oved(DefaultTreeModel.java:530)
    at javax.swing.tree.DefaultTreeModel.nodesWereRemoved (DefaultTreeModel.java:310)
    at javax.swing.tree.DefaultTreeModel.removeNodeFromPa rent(DefaultTreeModel.java:244)
    at GUI.buildTree(GUI.java:354)
    at GUI$3.actionPerformed(GUI.java:236)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Last edited by aussiemcgr; December 9th, 2010 at 03:53 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: JTree - Remove All Nodes

    I was hoping more for an something compileable, but there may be enough info in there. Try updating the model direction using model.insertNodeInto rather than adding the children direction to each other. This should fire the appropriate events to update the model.

Similar Threads

  1. [SOLVED] Jtree help
    By sman36 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 08:39 AM
  2. How to enumarete Network Nodes using Java
    By dilshadpaleri in forum Java Networking
    Replies: 0
    Last Post: September 7th, 2010, 06:45 AM
  3. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  4. Having trouble redirecting nodes
    By KingLane in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2009, 06:46 PM
  5. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 03:15 AM