-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
  • 166,747
  • 40
  • 210
  • 420
Java_Alert
  • 1,079
  • 6
  • 21
  • 49

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
  • 200,320
  • 28
  • 229
  • 974
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
  • 41,437
  • 4
  • 36
  • 60