I'm using a custom treeModel for an application I'm writing, and it's
all fine except for one quite trivial issue with the way the tree is
displayed.
I have a set of nodes which I always want to appear as a node rather
than a leaf even if they have no children, so isLeaf always returns
false, and getAllowsChildren returns true, getChildCount can return 0.
The tree draws OK, but empty folders initially draw in a way which
suggests they should have children under them and could be expanded
(Windows L&F shows a plus symbol, Mac has an arrow next to them). Once
you try to expand an empty folder the extra symbol dissapears, and I get
what I would have originally liked (a picture of a folder, but nothing
to click on to expand it).
What to I need to set in my Model to cause this to happen by default?
Cheers
Simon.
jonck@vanderkogel.net - 11 Apr 2005 10:29 GMT
> The tree draws OK, but empty folders initially draw in a way which
> suggests they should have children under them and could be expanded
Whether a node is drawn as a leaf or not depends on your isLeaf(Object)
method in the TreeModel. So the reason an empty folder is being drawn
as though it could be expanded is because you're telling your JTree
that the folder is not a leaf and can therefore be expanded.
If you want to ensure that an empty folder is not drawn as such that it
can be expanded, you'll have to put some logic in the isLeaf(Object)
method of your TreeModel that checks whether an Object is actually a
leaf or not.
Regards, Jonck
Simon Andrews - 11 Apr 2005 11:24 GMT
>>The tree draws OK, but empty folders initially draw in a way which
>>suggests they should have children under them and could be expanded
[quoted text clipped - 3 lines]
> as though it could be expanded is because you're telling your JTree
> that the folder is not a leaf and can therefore be expanded.
Saying the node is a leaf would provide different semantics than what
I'm after. I want the node to show as a folder, but the fact that it
contains no children should mean that it shows without the expansion
symbol. The Swing UI must know about this distinction as it does the
right thing once you've tried to exapand it.
Cheers
Simon.
Jonck - 11 Apr 2005 12:27 GMT
> Saying the node is a leaf would provide different semantics than what
> I'm after. I want the node to show as a folder, but the fact that it
> contains no children should mean that it shows without the expansion
> symbol. The Swing UI must know about this distinction as it does the
> right thing once you've tried to exapand it.
Ah yes, I see what you mean. Perhaps a work-around could be that you
test whether or not the node has children or not in the isLeaf method (
and return true if the node has no children) and then use a custom
TreeCellRenderer to draw the "leaf" as a folder in this particular case.
Not very elegant, but I cannot think of another solution at this time.
Regards, Jonck
Thomas Weidenfeller - 11 Apr 2005 10:32 GMT
> The tree draws OK, but empty folders initially draw in a way which
> suggests they should have children under them and could be expanded
> (Windows L&F shows a plus symbol, Mac has an arrow next to them). Once
> you try to expand an empty folder the extra symbol dissapears, and I get
> what I would have originally liked (a picture of a folder, but nothing
> to click on to expand it).
It is a long time ago that I saw this, so I don't remember all the
details. AFAIR this is a known bug. You might want to check Sun's
bugparade. Maybe there is a workaround listed, maybe upgrading to a
later Java version (what version do you use? 1.3.x?) should fix this.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Simon Andrews - 11 Apr 2005 11:32 GMT
>> The tree draws OK, but empty folders initially draw in a way which
>> suggests they should have children under them and could be expanded
[quoted text clipped - 7 lines]
> bugparade. Maybe there is a workaround listed, maybe upgrading to a
> later Java version (what version do you use? 1.3.x?) should fix this.
Oh dear (but thanks for the pointer):
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4200020
"State: Closed, will not be fixed" (and no work around other than a
vague statement saying you'd probably need to poke around in JTree).
The logic is that in some circumstances this would impose a large
overhead where applications dynamically load data following a
getChildCount call. However this means that it does the "wrong" thing
some of the time for everybody.
I suppose I could try expanding and collapsing every branch when I first
load the tree, but this seems like a really nasty way to achieve a
rather simple effect.
Simon.
Simon Andrews - 11 Apr 2005 11:55 GMT
>>> The tree draws OK, but empty folders initially draw in a way which
>>> suggests they should have children under them and could be expanded
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4200020
>
> "State: Closed, will not be fixed" (and no work around other than a
> vague statement saying you'd probably need to poke around in JTree).
The bug report said that "we delay calling getChildCount till the node
has been expanded once", so I thought I'd just do that programatically
and everything would be OK, so I added:
int rowCount = tree.getRowCount();
for (int i=1;i<=rowCount;i++){
tree.expandRow(i);
}
for (int i=1;i<=rowCount;i++){
tree.collapseRow(i);
}
..but with no effect! I removed the collapseRow() calls and I now see a
fully expanded tree, but still with the expansion symbols next to empty
folders.
I think a new bug report is called for, but can anyone think of a way to
work around this?
Cheers
Simon.
Christian Kaufhold - 11 Apr 2005 12:15 GMT
> I'm using a custom treeModel for an application I'm writing, and it's
> all fine except for one quite trivial issue with the way the tree is
[quoted text clipped - 10 lines]
> what I would have originally liked (a picture of a folder, but nothing
> to click on to expand it).
They *can* be expanded/collapsed even if the symbol is not shown.
http://groups-beta.google.com/group/comp.lang.java.gui/browse_thread/thread/6bb4
d3bbdf0a63b3/509838b5ce1cd71c
Christian
Simon Andrews - 11 Apr 2005 12:46 GMT
>>I'm using a custom treeModel for an application I'm writing, and it's
>>all fine except for one quite trivial issue with the way the tree is
[quoted text clipped - 14 lines]
>
> http://groups-beta.google.com/group/comp.lang.java.gui/browse_thread/thread/6bb4
d3bbdf0a63b3/509838b5ce1cd71c
That's exactly what I needed! Works a treat.
Cheers!
Simon.