Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: problems on implementing a JTree

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems on implementing a JTree

    Well I am currently designing a JTree that is dynamicly loaded from a given database.
    The way I did it is that I created a class that stores an id, a parent node, a list of child nodes, and the data. Then what I did was creating a list of this class from the original data and did this thing below.

    private void writeRTTree()
    {
    //Change the root node display.
    //Initialise the initial nodes.
    rtTreeNodes = new DefaultMutableTreeNode[rtNodesFile.nLines];
    for(int index = 0; index < rtNodesFile.nLines; index++)
    {
    //Create a node has the name of it's corresponding report type string.
    rtTreeNodes[index] = new DefaultMutableTreeNode(rtNodesFile.rtNodes[index].rtData);
    //Insert the node to it's appropriate place.
    if(rtNodesFile.rtNodes[index].rtParent == 0)
    {
    //Adds the node to the root.
    rtTreeModel.insertNodeInto(rtTreeNodes[index], rtTreeRoot, rtTreeRoot.getChildCount());
    }
    else
    {
    //Adds the node to the corresponding parent.
    rtTreeModel.insertNodeInto(rtTreeNodes[index], rtTreeNodes[rtNodesFile.rtNodes[index].rtParent - 1], rtTreeNodes[rtNodesFile.rtNodes[index].rtParent - 1].getChildCount());
    }
    }
    }

    The loading was sucessful but when I was trying to locate the user selected node by using "getSelectionPath()", I failed and whan i was trying to print it it returns null pointer exception. However this function works fine before I load the tree.

    I sorta knows that my problem is probably because those JTree nodes are created in a tree, but if I dont do that is there any ways I can create those nodes and make them able to respond to user's selection? Or is there any ways I do not need to correct my "writeRTTree()" function and that would be so much better.

    Thank You


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems on implementing a JTree

    do i need bumps here or not?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problems on implementing a JTree

    I was trying to locate the user selected node by using "getSelectionPath()", I failed
    Define failed. If you get exceptions or compile time errors, post them here. And it always helps to post an SSCCE that clearly demonstrates the basic premise of what you are attempting to accomplish.

Similar Threads

  1. [SOLVED] Need help with my JTree add JScrollPane
    By justyStepi in forum AWT / Java Swing
    Replies: 13
    Last Post: April 26th, 2012, 05:04 PM
  2. JTree DnD Problem
    By hafunui in forum AWT / Java Swing
    Replies: 3
    Last Post: August 12th, 2011, 10:33 PM
  3. JTree not expanding?
    By captain alge in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 03:32 AM
  4. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  5. [SOLVED] Jtree help
    By sman36 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 09:39 AM