Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtree problem

 
jono richy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I delete a node from a treemodel? I tried 2 delete one using treeModel.removeNodeFromParent(temp_node); but my temp_node does not contain a parent in it. This is because it was defined on its own has a DefaultMutableTreeNode temp_node; where I wanted to create a loop that gets each node from a parent and temporarily insert the object into the temp_node..





Where it gets the username i.e. temp = treeModel.getChild(online, i); how can I get both the parent and object of that specific node so that I can remove the node from its treemodel. Any comments?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT forum.
 
jono richy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any suggestions?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you're creating that extra node with the "real" node as its user data, and I don't understand why, given that you have, you don't just take the "real" node out using the getUserObject() method, and remove that from its parent. Either I'm missing something subtle, or you've got a serious misunderstanding of how TreeModels work.

Which do you think it is?
 
jono richy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i cant seem to find the code where you indicated that i used the method getuserobject(). do you know of a way where i can delete data off a treemodel?

i know what a treemodel is, it contains the "actual data" for a jtree which only displays that data from the treemodel. how can i delete the actual data node?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't use getUserObject(), but you could use it to get access to the real TreeNode, the one that does have a parent, the one you're actually trying to delete from the tree. But as I said, I'm still totally unclear as to why you're "hiding" that real TreeNode inside another TreeNode as its user object. Can you explain that to me? Because either that's your problem right there, or you're trying to do something special that I don't understand.
 
jono richy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which extra node are you talking about? the temp_node? because the treeModel.removeNodeFromParent(temp_node); can only delete varaiblaes that are of type node, not object, in which

//get the username
temp = treeModel.getChild(online, i);

returns a object. thats why i tried to store thsi object in a new node..
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK.

They defined the TreeModel interface so that it's possible to define a TreeModel whose nodes are not TreeNodes. I think it's a silly design and makes things harder -- and more confusing -- than they have to be.

For javax.swing.tree.DefaultTreeModel, anyway, getChild() returns a TreeNode -- a DefaultMutableTreeNode, unless you're putting some other kind of TreeNodes into your tree yourself. It's just that the type information is lost. You can get it back -- just cast that Object to a TreeNode:

DefaultMutableTreeNode nodeYouWantToRemove = (DefaultMutableTreeNode) treeModel.getChild(online, i);

and there you go!
[ May 01, 2006: Message edited by: Ernest Friedman-Hill ]
 
jono richy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
opps edited:

thanks it works!! but now i have another dilema! when the node is deleted. does the index of all of the remaining nodes stay the same? if so i need to re shuffle the whole nodes so that their is no null node in-between the other nodes.

ok for example, i have 4 nodes {alex, brian, steve, james}in offline parent; and none in the online parent.
when user brian comes online, the node named brian is removed from the offline parent and into the online parent. does the offline parent leave a gap like this

new results: online parent {brian} offline parent {alex, null, steve, james} ??

if this is the case, is it possible to move the values after brian, i.e steve, james so that it looks like this:

{alex, steve, james}


[ May 01, 2006: Message edited by: jono richy ]
[ May 01, 2006: Message edited by: jono richy ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the indices change. If you remove the second child, the third one becomes the new second one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic