How to create a JTree with multiple root node?

Discussion in 'Java' started by Allen, Jul 12, 2006.

  1. Allen

    Allen Guest

    I want to create a tree like eclipse package explorer. Who can tell me
    how to cope with root node? Thank you.
     
    Allen, Jul 12, 2006
    #1
    1. Advertisements

  2. 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.
     
    Brandon McCombs, Jul 12, 2006
    #2
    1. Advertisements

  3. Allen

    Rogan Dawes Guest

    tree.setRootNodeVisible(false);

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

    Rogan
     
    Rogan Dawes, Jul 12, 2006
    #3
  4. Allen

    Chris Smith Guest

    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.
     
    Chris Smith, Jul 12, 2006
    #4
  5. Allen

    Allen Guest

    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");
     
    Allen, Jul 14, 2006
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.