CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 1999
    Location
    North Sydney, NS
    Posts
    445

    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.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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?

  4. #4
    Join Date
    Dec 1999
    Location
    North Sydney, NS
    Posts
    445

    Re: JTree node to look like folder rather than file

    It works. Thanks a lot.
    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
  •  





Click Here to Expand Forum to Full Width

Featured