Java – way to have expander icons for multiple roots in a JTree

javajtree

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.

Best Answer

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.

Related Topic