File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes filtering N number of leaf nodes per parent node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "filtering N number of leaf nodes per parent node" Watch "filtering N number of leaf nodes per parent node" New topic
Author

filtering N number of leaf nodes per parent node

mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi. i have a filteredtreemodel that extends DefaultTreeModel

what i want to do is at the end of each parent node, i place a Jbutton as a leaf node that says show/hide.
when this button is clicked, it will only show like the 1st 5 leaf nodes if button labeled, hide. if it is labeled show, it will display all leaf nodes

my problem is that whenever i iterate from the parentnode using either getChildAt() or children() from the DefaultMutableTreeNode and i loop the nodes one by one, it doesn not seem to be in order

if it was displayed in order, this would be easy. however it is not and i cant understand why it's outputted in random and not in sequence as when you added them to the treemodel.

anyone got ideas? thanks


Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Hi Mark,
Please post a Short, Self Contained, Correct (Compilable), Example. Without that I can say only that there must be some problem in your code. The DefaultMutableTreeNode's documentation says the following:
Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.

So it puts the new nodes into the end of the list.
The getChildAt says:
Returns the child at the specified index in this node's child array.

I'm sure this class behaving according to it's documentation, so it will give back the nodes in the right order.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi miklos

this is my working code



im not sure if my filter tree model is the problem because when i run my app, it works fine either in show all or hide mode. the problem occurs when i click the button to either show or hide, it gives me an array index out of bounds exception. and i am not sure where in my filtertree class the problem lies. please advise. thank you

by the way, my jtree has checkbox leaf nodes in it, and the last leaf node for every parent node is a jbutton where the show/hide functionality happens. coz my leaf nodes may be lots of them so i want to be able to hide so only the 1st 5 leaf nodes of every parent node appears.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Please reread SSCCE and post according to that, so we can experiment with your code.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
sorry what does SSCCE mean? i searched for it here but it didnt give any results. thank you
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Short, Self Contained, Correct (Compilable), Example
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
just a quick quest. im wondering why defaultmutabletreenode's children() or getChildrenAt() do not return nodes according to the sequence when they were added.

if it was sequential it would be easy to locate which nodes should appear under which parent.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
They do return in the sequence they were added. Please try it without your FilteredTree code and you will see that it works fine. I'm almost sure that your code makes it invalid, but I won't spend my time debugging your code, till you won't post a runnable example.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi miklos. yes. i will post it. hopefully tomorrow night gmt+8. i did not bring my source with me right now. there are actually 6 files in total because my tree nodes are either jcheckbox and a jbutton as the last leaf node.

is it ok to post them all here? i might just rar them and host it some place else
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I think it's okay to paste that 6 file between code tags here.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi miklos. here are the codes. 7 files in all.













mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
what i want with my tree is that if i click the show button, it will change its status to HIDE, and will show all leaf nodes

in the CheckBoxNode class, i re-used the variable selected for the button so i could use it to base my show/hide state. but in the QueueCheckBoxNodeEditor where the button click action takes place, i dont think the "editor" object can be converted to class CheckBoxNode so im stuck with how to make this work.

and in the QueueTreeFilter class, if you system.out the nextNode object, it is not outputted in order. my logic was if it encounters a button, it will remove all leaf nodes before it except the 1st three.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I think one bad thing in your filtered tree is that you change the model without notifying anybody about it. You change the tree's structure from a tree cell editor. I think that is a bad idea, maybe you should use SwingUtilities.InvokeLater. I'm just guessing, so I hope some JTree guru will give the correct answer to your question. Till then I suggest you to check these examples which shows how can you make a tree which allows hiding/showing nodes.
http://forums.sun.com/thread.jspa?threadID=563514&forumID=57
http://www.kidslovepc.com/javatable/java-jtree-show-hide.shtml

mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hey thanks. i am using the 2nd link classes now. and i extended my CheckBoxNode class to inherit InvisibleNode

and in my main class, instead of the queuetreefilter i used the invisiblemodeltree. same sequence of code. but when i reload the tree model after setting the node invis and/or unsetting, the nodes are still there. they dont disappear.

i debugged the code, the visible attribute it set to invis when i i set it. the problem is when the filter is activated, it does not hide it
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi. i actually made this work now using the invisibleNode link you gave to me. i jut got one tiny little problem,
you see, if Location is the root node, it works with no problems

after I added two sets Location and Location 2 under root, the error pops up. java.lang.NullPointerException. and the stacktrace doesnt give me the line number where the error may be but it's within the java class

at javax.swing.plaf.basic.BasicTreeUI.completeEditing(BasicTreeUI.java:2014)

any idea?















 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: filtering N number of leaf nodes per parent node