|
-
June 10th, 2006, 09:44 AM
#1
JTree node to look like folder rather than file
Anybody know how to get a tree node to look like a folder when it has no children? Every time I create a node without children it looks like a file.
Thanks.
I know how to build. What to build is a completely different story.
-
June 10th, 2006, 11:09 AM
#2
Re: JTree node to look like folder rather than file
Not 100% sure on this but have you tried:
Code:
DefaultTreeModel model = (DefaultTreeModel)myTree.getModel();
model.setAsksAllowsChildren(true);
The API docs for the reciprocal method asksAllowsChildren() is:
Returns: true if only nodes which do not allow children are leaf nodes, false if nodes which have no children (even if allowed) are leaf nodes
Which I think means if your nodes allow children, but do not have any yet, they should be regarded as folders.
=====================================================
Sorry this doesn't work - see next post for a working solution
Last edited by keang; June 10th, 2006 at 12:16 PM.
Reason: Postscript added
-
June 10th, 2006, 12:14 PM
#3
Re: JTree node to look like folder rather than file
I've had a play and my previous suggestion doesn't work However I have worked out a way to do it
You need to create your own node class and tree cell renderer class. The node class doen't need to do anything other than extend your existing node class eg DefaultMutableTreeNode. The cell renderer (see below) then checks for an instance of your node class and always assigns the folder icon.
Code:
class FolderNode extends DefaultMutableTreeNode
{
FolderNode(Object obj)
{
super(obj);
}
}
class NodeRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
if (value instanceof FolderNode ) {
if ( expanded ) {
setIcon(openIcon);
}
else {
setIcon(closedIcon);
}
}
return this;
}
Does anyone know of an easier way than this?
-
June 10th, 2006, 03:59 PM
#4
Re: JTree node to look like folder rather than file
I know how to build. What to build is a completely different story.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|