Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2007

Tip: Looking for answers? Try searching our database.

how to have empty nodes in a jtree that still have an expansion control

Thread view: 
Brandon McCombs - 01 Feb 2007 06:19 GMT
It seems in Sun's infinite wisdom that a node in a JTree must have at
least 1 child in order for there to be an expansion control next to the
node. I don't want to load my tree structure all at once and so I'm
getting each node's children a node at a time when it is either selected
or expanded. But I still want the stupid expansion control so I'm
creating a fake node temporarily.

This does the job except that when a node is expanded for the first time
I get a quick blink where the dummy node is being deleted before the
real nodes show up.  I don't know why this happens since I'm only
reloading the tree at the selected node one time so I was wishfully
thinking that I wouldn't even notice 1 node being removed and a bunch of
others being added but I do.  Do I have to go to the trouble of creating
my own tree expansion listener (disable the listener, then remove the
dummy node, reload the tree at the selected node, add the real nodes,
then enable the listener and reload again?) just for this stupid little
feature that should already be there?  What can I do to fix it?

thanks
Michael Rauscher - 01 Feb 2007 06:58 GMT
Brandon McCombs schrieb:
> It seems in Sun's infinite wisdom that a node in a JTree must have at
> least 1 child in order for there to be an expansion control next to the
> node.

AFAIK it should be enough for TreeModel's isLeaf method to return false.

Bye
Michael
Brandon McCombs - 01 Feb 2007 15:09 GMT
> Brandon McCombs schrieb:
>> It seems in Sun's infinite wisdom that a node in a JTree must have at
[quoted text clipped - 5 lines]
> Bye
> Michael

Well since I'm only grabbing a node's children when the node is selected
or expanded the JTree probably is using the isLeaf method from the
TreeModel to not draw an expansion control next to the node because at
that point until the node's children are retrieved it is a leaf node
which is why I have to put in a fake node so that it isn't a leaf node
any longer. Then I have to go to the trouble of deleting the node and
still make the application behave properly. Deleting it is easy but in
my OP I stated the trouble I ran into when I attempted that.
Edwin Dankert - 01 Feb 2007 16:08 GMT
> Well since I'm only grabbing a node's children when the node is selected
> or expanded the JTree probably is using the isLeaf method from the
[quoted text clipped - 4 lines]
> still make the application behave properly. Deleting it is easy but in
> my OP I stated the trouble I ran into when I attempted that.

Sun, has provided all the functionality to implement this relatively
simply, I think however that the route you have chosen might not be
the most effective one.

Most Swing components are controlled by their models so, I would
suggest you try and implement the TreeModel or implement the TreeNode
(which is often the easier route) or a combination of both, this would
allow you to return false for the isLeaf() method. I would also
recommend to implement a TreeWillExpandListener to perform the look-up
of the child TreeNodes before the tree is expanded.

Regards,
Edwin
--
http://www.edankert.com/
Brandon McCombs - 01 Feb 2007 23:35 GMT
>> Well since I'm only grabbing a node's children when the node is selected
>> or expanded the JTree probably is using the isLeaf method from the
[quoted text clipped - 12 lines]
> suggest you try and implement the TreeModel or implement the TreeNode
> (which is often the easier route) or a combination of both, this would

My custom node already extends DefaultMutableTreeNode and I'm using the
DefaultTreeModel for the tree itself.

> allow you to return false for the isLeaf() method. I would also
> recommend to implement a TreeWillExpandListener to perform the look-up
> of the child TreeNodes before the tree is expanded.

I'll try that since Rogan also suggested that. Hopefully that will solve
the problem.

> Regards,
> Edwin
> --
> http://www.edankert.com/
Brandon McCombs - 02 Feb 2007 01:39 GMT
>>> Well since I'm only grabbing a node's children when the node is selected
>>> or expanded the JTree probably is using the isLeaf method from the
[quoted text clipped - 22 lines]
> I'll try that since Rogan also suggested that. Hopefully that will solve
> the problem.

I seemed to have fixed it by utilizing my own variable that keeps track
of the number of children that I already had in my node class.  By
setting that variable to 1 when the children of a node were retrieved
and then setting it with the proper value when the children for those
children were retrieved (and overriding the isLeaf() method) I got it to
work. Since it didn't require adding fake children nodes it avoids the
problem of the tree jumping a bit when the fake child is removed just
before the real ones are added.
Rogan Dawes - 01 Feb 2007 17:29 GMT
> Brandon McCombs schrieb:
>> It seems in Sun's infinite wisdom that a node in a JTree must have at
[quoted text clipped - 5 lines]
> Bye
> Michael

Correct. Implement your own TreeModel, and add a TreeWillExpandListener.

See:

http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html

for a good tutorial on how to do this.

Rogan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.