Is there a way to have expander icons for multiple roots in a JTree?
Asked Answered
G

1

8

I have a JTree with multiple "roots" (Of course, I actually have an invisible real root with multiple children).

The nodes expand and collapse on double click, but there's no visual indication that you can do this as there is no expander icon.

This is made worse by the fact that the tree is collapsed by default, but expanding the "roots" doesn't really help, as each has many children and it would look cluttered.

Is there a way to display the expander icons without making the real (and utterly valueless) root visible?

Any other suggestions to make the display clearer welcome.

Gabardine answered 12/1, 2009 at 16:6 Comment(0)
N
15

Would tree.setShowsRootHandles(true) be a good way to display those "expander icons" ?

A tree typically also performs some look-and-feel-specific painting to indicate relationships between nodes. You can customize this painting in a limited way.

  • First, you can use tree.setRootVisible(true) to show the root node or tree.setRootVisible(false) to hide it.
  • Second, you can use tree.setShowsRootHandles(true) to request that a tree's top-level nodes — the root node (if it is visible) or its children (if not) — have handles that let them be expanded or collapsed.

Check also your look and feel to be sure what the renderer does with your tree.

Nada answered 12/1, 2009 at 16:21 Comment(1)
Perfect, thanks! I totally missed that from the docs - probably because they're not actually "root handles" at all.Gabardine

© 2022 - 2024 — McMap. All rights reserved.