How to Get Root Path in JTree

Discussion in 'Java' started by Jason Cavett, Feb 7, 2007.

  1. Jason Cavett

    Jason Cavett Guest

    I am currently adding nodes a JTree. I want the user to have the
    ability to add a node to the root node which is *not* visible (AKA - I
    had the root node). Basically, this means that when the user has no
    nodes selected and they try to add a new node (via a menu), it should
    automatically go to the root node.

    The problem I'm having is that although I can get the path for the
    root, DefaultTreeModel *always* throws an exception because of this
    method...

    --
    public void nodesWereInserted(TreeNode node, int[] childIndices) {
    if(listenerList != null && node != null && childIndices != null
    && childIndices.length > 0) {
    int cCount = childIndices.length;
    Object[] newChildren = new Object[cCount];

    for(int counter = 0; counter < cCount; counter++)
    newChildren[counter] = node.getChildAt(childIndices[counter]);
    fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
    newChildren);
    }
    }
    --


    Basically, what happens is that, when the DefaultTreeNode tries to
    fire that a new node was inserted, it is still holding the "path" to
    the root node. Since the root node isn't showing, that path's row
    value is -1.

    Is there any way to add a new node to the root node, even though that
    root node is not visible?


    Thanks.
    Jason Cavett, Feb 7, 2007
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > I am currently adding nodes a JTree. I want the user to have the
    > ability to add a node to the root node which is *not* visible (AKA - I
    > had the root node). Basically, this means that when the user has no
    > nodes selected and they try to add a new node (via a menu), it should
    > automatically go to the root node.
    >
    > The problem I'm having is that although I can get the path for the
    > root, DefaultTreeModel *always* throws an exception because of this
    > method...


    Please provide the exception.

    Bye
    Michael
    Michael Rauscher, Feb 8, 2007
    #2
    1. Advertising

  3. Jason Cavett

    Jason Cavett Guest

    On Feb 8, 4:46 am, Michael Rauscher <> wrote:
    > Jason Cavett wrote:
    > > I am currently adding nodes a JTree. I want the user to have the
    > > ability to add a node to the root node which is *not* visible (AKA - I
    > > had the root node). Basically, this means that when the user has no
    > > nodes selected and they try to add a new node (via a menu), it should
    > > automatically go to the root node.

    >
    > > The problem I'm having is that although I can get the path for the
    > > root, DefaultTreeModel *always* throws an exception because of this
    > > method...

    >
    > Please provide the exception.
    >
    > Bye
    > Michael


    Thanks for the response. After working with my code a bit more, I
    found a much more elegant solution which didn't require any of this.
    Jason Cavett, Feb 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    2
    Views:
    4,485
  2. Replies:
    1
    Views:
    3,120
    Andrew Thompson
    Jan 3, 2005
  3. Allen
    Replies:
    4
    Views:
    12,644
    Allen
    Jul 14, 2006
  4. Replies:
    2
    Views:
    2,099
  5. jumpz
    Replies:
    3
    Views:
    612
    Jean-Baptiste Nizet
    Nov 12, 2007
Loading...

Share This Page