aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes prevent + sign to vanish in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "prevent + sign to vanish in JTree" Watch "prevent + sign to vanish in JTree" New topic
Author

prevent + sign to vanish in JTree

pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
I have a jtree which is created on 'As needed' basis. i.e, when the user expands the particular node, I make a database call in TreeWillExpandListener and add the children to their parent.
Say, the user has clciked on the + sign besides the node and at that point of time the database is down and I get an exception. when I get the exception, the jtree assumes that there are no children and removes the +. Is it possible to prevent '+' being removed when I get exception from backend?
It would be great if someone has a quick fix for this cos I need this problem to be fixed fast.
Thanks a loot.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Quick fix...

Make a subclass of DefaultTreeModel, override reload() to be public, and override isLeaf() to always return false. Then add a TreeWillExpandListener to the tree to call reload() on the tree model whenever the tree will expand... the current selection is drawn without a '+', but it is added back once the focus changes. This solution may be short term, since constantly reloading the tree will probably cause a slow-down, especially since you are loading your data from a data base... though this should work out fine for testing...



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
raj madhuram
Ranch Hand

Joined: Oct 10, 2001
Posts: 71
[OFFTOPIC]
import sun.security.krb5.internal.crypto.d
Using eclipse?
[ January 20, 2003: Message edited by: raj madhuram ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Oops... yeah...
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Actually, I have subclass DefaultMutableTreeNode and overridden the isLeaf() to show the + sign.
public boolean isLeaf() {
return !getAllowsChildren();
}
If I call,
((DefaultTreeModel)jtree.getTreeModel()).setAsksAllowChildre(true),
will it do my task?
Thanks.
[ January 21, 2003: Message edited by: pavan in ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

It will show the '+' the first time, but you will have to keep reloading the tree if you want it to always show the '+' sign.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: prevent + sign to vanish in JTree
 
Similar Threads
sorting jtree
Expand Lazy Loaded JTree
Lazy fetching JTree
JTree problem
JTree