Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jtree

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.

like image 463
Draemon Avatar asked Jan 12 '09 16:01

Draemon


People also ask

Which node is one which is under a collapsed ancestor?

A hidden node is one which is under a collapsed ancestor.

How does JTree work?

JTree class is a powerful Swing component for displaying tree-structured data. Like all Swing components, JTree relies on a separate model object to hold and represent the data that it displays. Most Swing components create this model object automatically, and you never need to work with it explicitly.


1 Answers

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.

like image 79
VonC Avatar answered Oct 06 '22 02:10

VonC