JTree problem with leaf and visibility!

I have two problems:

If I remove the last leaf from a node, this node automatically has the appearance of a leaf. Is there a way, that the node keeps his appearance as a node?

I am able to set the roof of my JTree invisible but if I try to set a node invisible, nothing happens. Is it possible to hide a node?


ziabAsked:
Who is Participating?
 
mwibbelsConnect With a Mentor Commented:
Thanks, good luck on solving the rest of the problem.

0
 
heyhey_Commented:
you can use your own cell renderer and render your favourite node Images.
the easiest way to 'hide' a node is to remove it from the JTree.
0
 
mwibbelsCommented:
If you're using a DefaultTableModel (descendant) you can use setAsksAllowsChildren(true). In that case only nodes that are not allowed to have children will appear as leaves (each node's getAllowsChildren() is called).

If you're using your own TableModel, you nead to override 'isLeaf()' to return false in the cases where you want a node to appear as a node, even if it has no children.

Don't know about hiding nodes ...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ziabAuthor Commented:
Thanks mwibbels,

that works, but there is comes another problem: The small standard icon appears, which normally changes depending if a node is expanded or not - but for the nodes, which have no leafs (and now appear correctly as nodes), this image is first there and then disappears, when the user clicks on it, that is not correct.
Is there a way to control the appearance of a JTree without this effect?


0
 
mwibbelsCommented:
That is caused by the (Basic)TreeUI. If you look in the code for javax.swing.plaf.basic.BasicTreeUI you'll see that it says in method paintExpandControl (which draws the icon you're talking about):

if (!isLeaf && (!hasBeenExpanded || treeModel.getChildCount(value) > 0)) {

So the icon will only be drawn if the node is no leaf and
1) has children, or
2) has never been expanded

This is exactly the behavior you describe, Swing regards it as a feature (you obviously see it as a bug).
To fix this you'd probably have to provide your own TreeUI. This is not a problem when your application uses a fixed L&F (simply override the TreeUI class for that L&F and call JTree.setUI(...) ). When the L&F is not fixed, it becomes annoying, because you'd have to subclass all TreeUI's.

Maybe someone else has a better idea?

0
 
heyhey_Commented:
you have to implement YOUR OWN TreeCellRenderer

I've posted some (working) code at
http://www.experts-exchange.com/comp/lang/java/Q.10125193

I suppose that it's much easier to make a JDK1.2 example (because it has some nice new classes)

0
 
ziabAuthor Commented:
Thanks mwibbels,

I don't know how to overwrite the class TreeUI. Please, can you show me an example for a solution of this problem.



0
 
mwibbelsCommented:
Sorry, don't have the time. You could look at heyhey_'s suggestion, although I'm not completely sure if that will change the icon's were talking about (It does allow you to change the node icons, don't know about the 'outline' icons).

You might take a look at http://java.sun.com/products/jfc/tsc/friends/sce/sce.html
0
 
ziabAuthor Commented:
hy again,

still not finished with my problem, but I managed to ovewrite this line

if (!isLeaf && (!hasBeenExpanded || treeModel.getChildCount(value) > 0))

to

if (!isLeaf && treeModel.getChildCount(value) > 0))

and had succes; the little icon did change his behaviour as I wanted hin to.
But my problem is not solved, because there are some leafs, that I would like to diplay as leafs and some other leafs, which are empty folders and I would like to display them as folders.

I'll work on it, but for this question I could give you the points, if you lock the question; your hints were usefull for my understanding.

Bye ziab
0
 
ziabAuthor Commented:
Thanks and bye!
0
All Courses

From novice to tech pro — start learning today.