DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004

    JTree & iterating through all selected Nodes

    Hi all,

    I'm looking for a means to identify all selected nodes within a JTree so that I can extract the object that I used to build each node. I have the JTree set-up and displaying properly. Single node selection works, but the program gets "confused" when multiple nodes are selected. It will properly extract the first and second node, and then all subsequent nodes are the same as the second node.

    What am I doing wrong?
    TreePath[] nodes = tree.getSelectionPaths();

    int[] dwgs = new int[nodes.length];
    for(int i = 0; i < nodes.length ; i++)
    TreePath temp = nodes[i];

    Object tempObj = tree.getLastSelectedPathComponent();

    DefaultMutableTreeNode treNode = (DefaultMutableTreeNode)tempObj;

    Drawing dwg = (Drawing)treNode.getUserObject();

    System.out.println("Found Dwg ID #" + dwg.idNumber()); // bit of debugging here

    dwgs[i] = dwg.idNumber();



    I don't want to use the TreeSelectionListener interface as I already have this doing other work in the program (displaying additional information on node selection).

    Any help would be appreaciated.

    James D.

  2. #2
    Join Date
    Nov 2003
    TreePath temp = nodes[i];
    // here you get one of the selected nodes (actually the path to it)

    Object tempObj = tree.getLastSelectedPathComponent();
    // but you never use that node in the loop, except at the end to deselect it.
    // I would replace this line of code by:

    Object tempObj = temp.getLastPathComponent()

  3. #3
    Join Date
    Feb 2004
    That's the ticket!! Thanks for your help.

    (I've really got to get my work to get me a proper IDE instead of doing this in notepad!!!)

    Thanks again.

    James D.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.