Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTree problem with leaf and visibility!

Posted on 1999-08-02
10
Medium Priority
?
1,250 Views
Last Modified: 2008-02-07
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?


0
Comment
Question by:ziab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 1247494
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
 
LVL 2

Expert Comment

by:mwibbels
ID: 1247495
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
 

Author Comment

by:ziab
ID: 1247496
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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 2

Expert Comment

by:mwibbels
ID: 1247497
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1247498
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
 

Author Comment

by:ziab
ID: 1247499
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
 
LVL 2

Expert Comment

by:mwibbels
ID: 1247500
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
 

Author Comment

by:ziab
ID: 1247501
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
 
LVL 2

Accepted Solution

by:
mwibbels earned 360 total points
ID: 1247502
Thanks, good luck on solving the rest of the problem.

0
 

Author Comment

by:ziab
ID: 1247503
Thanks and bye!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question