File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

prevent + sign to vanish in JTree

 
pavan in
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
raj madhuram
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[OFFTOPIC]
import sun.security.krb5.internal.crypto.d
Using eclipse?
[ January 20, 2003: Message edited by: raj madhuram ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... yeah...
 
pavan in
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic