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.
Printable View
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.
Not 100% sure on this but have you tried:
The API docs for the reciprocal method asksAllowsChildren() is:Code:DefaultTreeModel model = (DefaultTreeModel)myTree.getModel();
model.setAsksAllowsChildren(true);
Which I think means if your nodes allow children, but do not have any yet, they should be regarded as folders.Quote:
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
=====================================================
Sorry this doesn't work - see next post for a working solution
I've had a play and my previous suggestion doesn't work :confused: 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.
Does anyone know of an easier way than this?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;
}
It works. Thanks a lot.