How to create a JTree with multiple root node?

A

Allen

I want to create a tree like eclipse package explorer. Who can tell me
how to cope with root node? Thank you.
 
B

Brandon McCombs

Allen said:
I want to create a tree like eclipse package explorer. Who can tell me
how to cope with root node? Thank you.

I can't say for sure (someone else can confirm) but from what I can tell
it looks like they have a single invisible root and any project/package
that you have defined in that view is just considered a 2nd level node
in the tree but the key is that the root itself is invisible so it just
gives the appearance that the others are at the root.
 
R

Rogan Dawes

Brandon said:
I can't say for sure (someone else can confirm) but from what I can tell
it looks like they have a single invisible root and any project/package
that you have defined in that view is just considered a 2nd level node
in the tree but the key is that the root itself is invisible so it just
gives the appearance that the others are at the root.

tree.setRootNodeVisible(false);

Then only your second level (and deeper) nodes are visible, and it looks
like you have a multi-rooted tree.

Rogan
 
C

Chris Smith

Allen said:
I want to create a tree like eclipse package explorer. Who can tell me
how to cope with root node? Thank you.

In SWT, or in Swing?

SWT: If using JFace (which I'd strongly recommend), then you have an
implementation of ITreeContentProvider, which extends
IStructuredContentProvider. You will implement getElements from the
latter class. Simply return all the top-level items as elements:

Swing: Swing's JTree must have a single root, but you can configure the
tree not to show it. Call setRootVisible(false) on the JTree object.
 
A

Allen

Thank you!

Yes, I call javax.swing.JTree setRootVisible(false) to hide the root
node.
But there comes another question. The first level, i.e. multiple roots
level, has not angled
line, while the other levels have. It is uneasy to differ from root
nodes from root child nodes on the UI. So how to show angled line for
root nodes?

putClientProperty("JTree.lineStyle", "Angled");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top