-1

Is it possible to make a JTree without leaf? If it is possible then please tell me way.

JTree image

I want to convert these highlighted leafs as the folder or parent.

If you want any thing else apart from it then please let me know.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Java_Alert
  • 1,159
  • 6
  • 24
  • 50

2 Answers2

2

As shown in this FileTreeModel, isLeaf() should return false and getChildCount() should return 0 for directories. The result is illustrated here; although not apparent, the test directory is empty.

@Override
public boolean isLeaf(Object node) {
    File f = (File) node;
    return !f.isDirectory();
}

@Override
public int getChildCount(Object parent) {
    File f = (File) parent;
    if (!f.isDirectory()) {
        return 0;
    } else {
        return f.list().length;
    }
}

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I think if you always return true from isLeaf in your TreeModel but return 0 from getChildCount for your leaf nodes you'll get what you want.

Tom
  • 43,583
  • 4
  • 41
  • 61